The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
python 2.4 on SCO 5.0.6 ananth_ak SCO 0 02-21-2008 07:01 AM
Foresight: python iBot Security Advisories (RSS) 0 02-12-2008 01:30 PM
what is python? kprescod4158 Shell Programming and Scripting 1 11-20-2007 08:58 PM
Python 2.5 must die. Dbecker UNIX for Dummies Questions & Answers 2 01-30-2007 12:35 PM
Python Help MAdSAcRAfICe UNIX for Dummies Questions & Answers 1 08-12-2002 03:20 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-12-2002
Furtoes00 Furtoes00 is offline
Registered User
  
 

Join Date: Oct 2001
Posts: 18
Post Python

I have written a small practice Python script to determine the prime numbers between 2-10, for practice. Although I have pored over this script numurous times, I have not found the problem.

The problem is: Python will not print "is a prime" when a number is a prime. After examining the code below, this will make sense..

Code:
for i in range(2,10):
	print
	print i,"-",
	for j in range(2,i):
		if (i % j) == 0:
			print j,
		else:
			if j == i + 1:
				print "is a Prime"
On execution, it prints this:

Code:
2 -
3 -
4 - 2
5 -
6 - 2 3
7 -
8 - 2 4
9 - 3
Note:I am using the Windows 98 IDLE GUI. I am not sure if this is affecting it..

I believe line 8 may be a problem. I am not sure whether to use "j - 1 == i" or j == i + 1".

Thank you,
Furtoes00

Last edited by Furtoes00; 04-13-2002 at 06:38 AM..
  #2 (permalink)  
Old 04-14-2002
thehoghunter
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
First - I have no idea what Python is.
Second - that means I have no idea of it's syntax.

BUT -

As Spock would say "Logic suggest..."

This code you posted:

if (i % j) == 0:
print j,
else:
if j == i + 1:
print "is a Prime"


IF it prints j, you will never go to the else. Logic is logic -

if (true) then do this ELSE do this - you need to change this so it looks at each separately or looks at the second within the if

if (true) then
print j
if (true) then
print "is a Prime"
  #3 (permalink)  
Old 04-16-2002
Furtoes00 Furtoes00 is offline
Registered User
  
 

Join Date: Oct 2001
Posts: 18
That does make sense; here is the modified code:

Code:
for i in range(2,10):
	print
	print i,"-",
	for j in range(2,i):
		if (i % j) == 0:
			print j,
		if j == i + 1:
			print "is a Prime"
However, that does not work, either!!
  #4 (permalink)  
Old 04-16-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
I too do not know python. But it looks like you line "for j in range(2,i)" will vary j starting at 2 and going up to either i or i-1, I'm not sure which from this thread. But either way, it looks like your final test in the loop is to see if j equals i+1. How can that ever happen?

In any event, your problem isn't python, it's the algorithm. Your inner loop is finding factors. Before you enter that inner loop, set "nfactor=0". Each time you find a factor and do the "print j", also increment nfactor. After the inner loop finishes, test nfactor...if it's zero, "print prime".
  #5 (permalink)  
Old 04-27-2002
Furtoes00 Furtoes00 is offline
Registered User
  
 

Join Date: Oct 2001
Posts: 18
I tried that before; it didn't work. Neither

i++

nor

i += 1

nor

i = i + 1

works.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0