Note that there are some explanatory texts on larger screens.

plurals
  1. POprime number generator in python: accumulation of numbers
    primarykey
    data
    text
    <p>My generator works great: I've tested it many times. Only, there is a problem: as the numbers increase, as one might believe, the program becomes slower. I have thought up of a way to do this, but don't know how, as I have only started with python not too long ago.</p> <p>My generator looks like this:</p> <pre><code> while 0==0: i=input('Enter the number for which all previous shall be tested for primality: ') n=0 a=[0,1,2] while n&lt;=i: n=n+1 for b in range(2,n): if n%b==0: break if b==(n-1): a.append(n) print a` </code></pre> <p>I have found that if I move the a=[0,1,2] to the space before while 0==0, it accumulates all numbers from previous uses while the program is running. What i want to change about this is that as a accumulates prime numbers, it uses those to catch up to the next unknown number. For example, lets say that I wanted all of the prime numbers up to 100. Then, I wanted all of the prime numbers up to 200. Instead of recalculating the prime numbers up to 100, I want to program to skip those and continue from the first prime number after 100. </p> <p>Any advice will be really appreciated, and I am using 2.7 Python.</p> <pre><code>a = [2,3,5,7,11] while 1: b = input('Enter the number for which all previous shall be tested for primality: ') c = len(a) d = 0 isprime = True while b&lt;=a[c-1] and not d==c: if b==a[d]: print a[0:d] if d==(c-1) and not b==a[d]: break d = d + 1 while b&gt;a[c-1]: d = 0 print a[c-1] if b%a[d]==0: isprime = False break while a[d]==a[c-1]: f = a[c-1] + 2 for g in range(f,b,2): if b%g==0: isprime = False break if isprime: a.append(b) print a </code></pre> <p>Alright, I made this program to work so that as the prime numbers are found, they are stored and used for the next set of prime numbers. With this let's say that I wanted to find the primes up to 1000. The program computes the primes. Then, I want to know the primes up to 2000. Well, since the program has already found the primes up to 1000, no need to reproduce them, so it takes all of the prime less than or equal to the highest number is the input, then finds what is left by dividing the new numbers by the known primes. It then adds the new primes to a, and continues.</p> <p>Only thing is, there is a problem. It doesn't want to work the way I planned, and I am working on trying to fix it. Maybe you guys can pitch in and see what is wrong?</p> <p>Alright, i have edited the code so that it runs faster: </p> <pre><code>While 1: i=input('Enter the number for which all previous shall be tested for primality: ') n=0 while n&lt;=i: n=n+1 a=int(n**.5) for b in range(2,n): if n%b==0: break if b==a: print n break </code></pre> <p>So far, this program runs in the fraction of the time as my original and those that i have tried. In a test I preformed, I had it and my first algorithm find all primes to 100000. My first algorithm took a tad bit over 4 minutes, unlike my new program, which took approximately 1 minute and 40 seconds. Quite the upgrade, if I may say so myself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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