December 9th, 2012, 04:50 PM

Help me get started
Hi everyone 
I'm getting started with Python using the MIT Openware Course that I'm sure many of you have seen.
One of the first assignments is to create code that generates prime numbers.
I am having difficulty figuring this out. Here is what I have:
Code:
#Makes a list of prime numbers between 3 and 10)
for candidate in range(3, 10):
divisor=1
while divisor<candidate1:
divisor=divisor+1
answer = candidate/divisor
remainder=candidate%divisor
print candidate,"/",divisor,"=",answer, " remainder:",remainder
if remainder==0:
print candidate, "divides evenly by", divisor
if remainder!=0:
print candidate, "does not divide evenly by", divisor
From this output, I can figure out which are primes, but how do I gather together the information about the numbers that do not divide evenly by ANYTHING? I feel like I need another embedded loop here but am not sure how to do it.
I know there are dozens of different codes for finding primes online, but I am not interested in copying somebody's else's, I'd like to see the steps of how I can build my own program.
This is not homework, so don't feel guilty about helping me! I am a literature teacher learning Python on my own for pleasure
December 9th, 2012, 05:17 PM

You don't need another loop, you just need a flag to keep track of whether any remainders are zero, for each candidate. The flag is reset to True for each candidate, and set to False if it is found to be divisible by something.
Code:
#Makes a list of prime numbers between 3 and 20)
for candidate in range(3, 20):
divisor = 1
isPrime = True
while divisor < candidate  1:
divisor = divisor + 1
answer = candidate / divisor
remainder = candidate % divisor
#print(candidate,"/",divisor,"=",answer, " remainder:",remainder)
if remainder == 0:
#print(candidate, "divides evenly by", divisor)
isPrime = False
else:
#print(candidate, "does not divide evenly by", divisor)
pass
if isPrime:
print("This is a prime: ", candidate)
This is a prime: 3
This is a prime: 5
This is a prime: 7
This is a prime: 11
This is a prime: 13
This is a prime: 17
This is a prime: 19
Be liberal with spaces, it makes code easier to read and spot mistakes;
You don't need a 2nd 'if remainder', just else;
Python3 print() requires brackets so you might as well include them.
Make the docs your constant companion
Last edited by AndrewSW; December 9th, 2012 at 05:25 PM.
December 9th, 2012, 05:36 PM

I tend to use Komodo Edit for Python but there are many alternatives.
December 9th, 2012, 06:40 PM

You guys are awesome! After getting your help, I was able to do the second stage of the task, to compute the 1000th prime number. I'm sure this isn't the most elegant way of doing it, but at least it works...
Code:
#Finds the 1000th prime number
number_in_sequence=1 #because 2 itself is the first prime and we won't test it
for candidate in range(3, 100000, 2):#start with 3, stop at a big number, test odd numbers only
divisor=1
isPrime = True#create variable isPrime
while divisor < candidate1: #test all possible divisors lower than the candidate
divisor = divisor + 1 #keep increasing the divisor to test them all
remainder = candidate % divisor #remainder tells us if it's prime
if remainder == 0:
##print candidate, "divides evenly by", divisor
isPrime = False #tells us it can't be prime, loops
##print candidate, "is not a prime"
else:
##print candidate, "does not divide evenly by", divisor
if isPrime == True:
pass
if isPrime == True:
number_in_sequence = number_in_sequence+1
##print candidate, "is a prime number. It's no. ", number_in_sequence
if number_in_sequence == 1000:
print candidate,"is the thousandth prime number."
break
December 9th, 2012, 06:51 PM

Try to keep your inline comments further to the right (or preferably on separate lines) as they obscure the code.
[I didn't know they had squirrels in Florida? Perhaps they were imported..]