First Program Bug (Newbie)

Benjamin Serrato benjamin.serrato at
Tue Mar 18 01:03:25 CET 2008

I Found It!! The following was a post asking for help finding a bug. I 
thought I needed help with my syntax, but just before sending I found 
the bug on line 13. Line 13 should read: "base = 2". I would still 
appreciate any comments on my program. Maybe there is a better way to do 
it that I didn't think about. I know it's not that interesting but it's 
my first one.

I'm almost halfway through an online tutorial I found on the 
site and decided to stop and write a program for myself. I got it in my 
head to write a program to print all the primes; the idea came from 
another tutorial. The program prints some non-prime numbers. In a 
comparison to lists of primes the program prints eight non-primes for 
numbers less than 150. Those numbers are [27,35,87,95,119,123,143,147]. 
Here is the program.

base = 2
candidate = 3

while True:
	while candidate % base != 0:
		base = base + 1
		if base > (candidate / 2):
			print candidate
			candidate = candidate + 1
			base = 2
		candidate = candidate + 1

The following is a rundown of the program.

candidate: A possible prime number.
base: Start point for divisibility testing.
outer while loop: Causes the program to loop.
inner while loop: Obviously, checks if 'candidate' mod 'base' equals 
zero. If not base is incremented by one then 'if loop'.
if loop: After base has been incremented this checks whether all the 
possible divisors have been used up. If they have then 'candidate' must 
be prime. So, candidate is printed, a new candidate is chosen and the 
base is reset.
else loop: If 'candidate' mod 'base' equals zero, then 'candidate' is 
not prime and a new candidate is chosen.

I realize yall probably didn't need all that.

At first I tried to use 'return' on the 'else' block to cause the 
program to loop, but I don't understand 'return' yet and that didn't 
work. So, I put the rest into another while loop and was really happy to 
find it worked but the program prints some non-prime numbers.

Thanks, Benjamin Serrato

P.S. What is the chance I'll get spam for using my real email address? I 
currently don't get any so...

More information about the Python-list mailing list