[Tutor] Making a Primary Number List generator
Dave Angel
davea at davea.name
Sun May 12 00:34:07 CEST 2013
On 05/11/2013 04:44 PM, Daniel Magruder wrote:
> Dear Tutor at Python.org,
> I am new to the python language and have been teaching myself through various online resources. I found an exercise where I am to create a program that prints a list of the first 1000 prime numbers. After many attempts and looking at other answers for other ways around the problem (which I did not understand), I am seeking your guidance.
> Here is what I have so far. While I understand there are many ways to do this, I would like advice on how to finish this program based on the initial path I started out on.
>
> def counting_primes():
> primelist = [] # I want a list of 1000 primes
> prime = f(0) # so that when I divid I get a true non int answer
> x = 1 # to test and make sure no other number goes into prime besides prime and one
> while len(primelist)<1000: # start a loop for as long as my list is not 1000 items in length
> if prime % x == num and x < prime:
> x = x + 1 # keep the dividing loop going until x and prime are equal
> if prime % x == 0: and prime != x: # if prime is not yet a prime number i.e. 8 and x is a number like 4, this prevents false positives
> x= x+1
> if prime = x:
> primelist = primelist + x # add x to my list of primes
> prime = prime + 1 # check to see if next number is prime
>
> return primelist
>
> Please walk me through any logical errors, I really want to understand this.
First you have to add a top-level call to this function. Otherwise the
program does nothing useful.
myprimes = counting_primes()
print len(myprimes) #how many did we find ?
print myprimes[:10] #what are the first ten ?
Then you have to eliminate your two syntax errors. There's only one
colon in an if statement, and it's at the end. And you cannot use = for
comparison, that's what == is for.,
Then you have to eliminate the references to undefined functions and
variables. There's no f() defined in your code, and all you want is 0,
so simply set prime = 0, not f(0). Actually, you probably want to set
prime to 2, as zero and 1 could be problematic. Once it works, you may
want to see if you could start at 1 instead. You have no num defined in
your code, so you'll have to decide what was actually intended there.
You now have a function which never returns, but at least it executes.
You have one loop which will only exit when the size of primelist
reaches 1000, and inside it, logic will will not add to that length.
You don't have the necessary loop inside this one to test the candidate.
If I were you, I'd break this code into two functions. The first one
will check a given number to see if it is prime, and return True or
False. The second one will repeatedly call the first on successively
higher numbers till it has enough. As it stands now, you have far too
much logic incrementing x and prime, and nothing that resets x back to 2
for the next iteration.
So the two functions, in pseudocode, should be something like:
def testprime(candidate)
check all ints between 2 and candidate, and return true if any of
them divide the candidate evenly
Otherwise return false
def counting_primes()
build a list of primes, where a number is put in the list if
testprime() confirms that it is prime.
return the list once it's big enough
It's actually easier to write two functions this way than to get the
logic right for the nested loops that you otherwise need.
Note that testprime() can use a for-loop, it needn't look like
transliterated Fortran.
--
DaveA
More information about the Tutor
mailing list