- Sep 29, 2000
- 2,157
- 0
- 0
Argh, help again, we have to write a program which generates the first n primes and it has to generate them in linear time (Is this even possible?).
The specs of the assignment is that we have to write a function which has the following specs:
n___ p__ r
100 547 700000
200 1229 140000
400 2749 320000
800 6143 7000000
1600 13513 1500000
3200 29453 3300000
where n is the nth prime number, p is what the number actually is and r is how many "reductions" we can do to obtain this number.
This means I get roughly 240 reductions per number searched assuming I only calculate odd numbers.
Now, a multiplication takes 16 reductions, and addition takes 16, a gcd takes roughly 50 and a mod takes roughly 80 so I only have about 4 or 5 operations per number.
The best so far that I have managed is to do it in polynomial time but at 10 times the allowable time for even the smallest one so I have NFI how I can make this any faster.
BTW: The language being used is Haskell, a functional programming language but what I am doing right now is:
take a list of primes you already know and a test number.
if the test number is divisible by the first prime, go onto the next test number
otherwise, test if it is divisible by the next prime until you get to the prime that is more that the square root of the test number. If you have gotten this far, then the tst number is a prime so you can add it to the list of primes and start with the new test number.
Does anyone have a better algorithm?
The specs of the assignment is that we have to write a function which has the following specs:
n___ p__ r
100 547 700000
200 1229 140000
400 2749 320000
800 6143 7000000
1600 13513 1500000
3200 29453 3300000
where n is the nth prime number, p is what the number actually is and r is how many "reductions" we can do to obtain this number.
This means I get roughly 240 reductions per number searched assuming I only calculate odd numbers.
Now, a multiplication takes 16 reductions, and addition takes 16, a gcd takes roughly 50 and a mod takes roughly 80 so I only have about 4 or 5 operations per number.
The best so far that I have managed is to do it in polynomial time but at 10 times the allowable time for even the smallest one so I have NFI how I can make this any faster.
BTW: The language being used is Haskell, a functional programming language but what I am doing right now is:
take a list of primes you already know and a test number.
if the test number is divisible by the first prime, go onto the next test number
otherwise, test if it is divisible by the next prime until you get to the prime that is more that the square root of the test number. If you have gotten this far, then the tst number is a prime so you can add it to the list of primes and start with the new test number.
Does anyone have a better algorithm?
