Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can improve the function greatly by just taking every 2nd number and dividing by it. First, 1 isn't a prime, you shouldn't use it in that way. The reason for this is the prime factorization, that is unique for every number, like 9 = 3*3. If you would add 1 to your prime pool, 9 = 3*3, 9 = 3*3*1, 9=3*3*1*1, every one is a valid prime factorization, but it isn't unique anymore for every number. Second, you don't have to check the number with every natural number. If you think about the natural numbers, every second of them is even and divisable by 2. So, if a number is divisable by 4, it is per definition divisable by 2. You can reduce the amount of calculations you have to do by a factor of 2 if you use this property. Also, you seem to use a technique called "The Sieve of Erastothenes" <a href="http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" rel="nofollow">http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes</a> which just adds prime numbers to the pool, and checks if the next natural number is divisable by any of them. You can exploit that easily.</p> <pre><code>def buildPrimeList (): #One is not really a prime, so we cut it out. primeList = [2] possiblePrime = 3 print "To display all prime values less than or equal a number..." upperlimit = raw_input("Enter a number higher then 3 ") try: upperlimit = int(upperlimit) except: print "Sorry. You didn't enter a number." return while (possiblePrime &lt;= upperlimit): #lets check if the possible prime is divisable by any prime we already know. isPrime = True for prime in primeList: if(possiblePrime % prime == 0): #we can abort the check here, since we know already that this number can't be a prime isPrime = False break if (isPrime): primeList.append(possiblePrime) possiblePrime = possiblePrime + 2 return primeList print buildPrimeList() </code></pre> <p>This should work as expected.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload