Fastest code that print all prime numbers between two given numbers ?
Not necessary shortest.
Range may be any two numbers between 1 & 1000000000
Time limit: 6s
Source limit: 50000B
|
Fastest code that print all Not necessary shortest. Range may be any two numbers between Time limit: 6s Source limit: 50000B |
|||||||||||||||
|
J
I was surprised how quickly this worked when I gave it |
|||||||||||
|
C++This code comes in at under 14 seconds worst-case (min=0, max=1e9, with disabling the
|
||||
|
C
Uses the Sieve of Eratosthenes to generate the primes and outputs them as it's doing so. Will fare worse against cleverer algorithms when the start number is very large. Edit: I should note that I came up with this answer before the 6 second time limit appeared. My program meets that limit for max numbers up to around 100000000 but goes into a period of deep contemplation when you try 1000000000. :-S |
|||||||||||
|
|
tested my code on a windows machine powered by intel 720QM, I've only timed preprocessing, since writing (to console) it self takes large amount of time. c++ :
|
Could I get an explanation on how isprime(X) ((X)&1?(bank[(X)>>4]&(1<<(((X)&14)>>1))):(X)==2) works? – GigaWatt Jan 10 '12 at 21:01 |
Q (91)
Sample Usage:
|
||||
|
|
|
|||||
|
C++, 3.3 secondsOk, this version can make the 6 second limit by a good margin. By default it computes the sum of the indicated primes, just uncomment the
|
|||
|
|
|
|||||
|