![]() |
|
|
#12 |
|
Feb 2017
Nowhere
10010001000112 Posts |
The following might be quicker than plowing through all the primes from 10^6 to 10^7 - 1, because it only looks at 9000 candidates. OK, I also cheated by using ispseudoprime().
Code:
v=vector(9000);t=1;for(i=1,9,for(j=0,9,for(k=0,9,for(l=0,9,n=i*10^6+11*(j*10^4+k*10^2+l);if(ispseudoprime(n),v[t]=n;t++)))));w=vector(t-1);for(i=1,t-1,w[i]=v[i]);print(w) |
|
|
|
|
|
#13 | |
|
Jun 2003
5,051 Posts |
Quote:
![]() Actually, if you replace ispseudoprime with isprime, your code takes a whopping 6ms
|
|
|
|
|
|
|
#14 | |
|
Feb 2017
Nowhere
4,643 Posts |
Quote:
The number of primes between 10^6 and 10^7 is 586081. Dividing 586081 by 9000 gives 65.1201111. Dividing 385 ms by 6 ms gives 64.166666.So it looks to me like reducing the number of candidates is mainly responsible for the decrease in runtime. |
|
|
|
|
|
|
#15 | |
|
Aug 2006
3×1,993 Posts |
Quote:
|
|
|
|
|
|
|
#16 |
|
Banned
"Luigi"
Aug 2002
Team Italia
32×5×107 Posts |
|
|
|
|
|
|
#17 |
|
"Dana Jacobsen"
Feb 2011
Bangkok, TH
22×227 Posts |
Below 2^64, there shouldn't be any difference in isprime and ispseudoprime. isprime() calls BPSW_isprime() which calls BPSW_isprime_small().
The value of not running through all the primes is more obvious if we extend this to larger variants, e.g. 4 or 5 repeated digits. Both of these are similar and pretty fast. Code:
$ time perl -Mntheory=:all -E 'my(@v)=(1..9); for (1..5) { @v=map { my $t=$_; map {"$t$_$_"}0..9; } @v; } say scalar(grep { is_prime($_) } @v);'
40272
real 0m0.846s
user 0m0.770s
sys 0m0.065s
Code:
? v=vector(900000);t=1;for(x=1,9,for(i=0,9,for(j=0,9,for(k=0,9,for(l=0,9,for(m=0,9,n=x*10^10+11*(i*10^8+j*10^6+k*10^4+l*10^2+m);if(isprime(n),v[t]=n;t++)))))));w=vector(t-1);for(i=1,t-1,w[i]=v[i]);length(w) time = 934 ms. 40272 |
|
|
|
![]() |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| M Prime Numbers | Anirban | Information & Answers | 1 | 2018-07-02 05:26 |
| What can you do with 2 prime numbers? | VicDiesel | Programming | 12 | 2017-04-20 21:16 |
| Prime Numbers Or Not | Arxenar | Miscellaneous Math | 38 | 2013-06-28 23:26 |
| All odd numbers are prime | Citrix | Lounge | 5 | 2010-04-05 21:34 |
| Prime numbers | Unregistered | Miscellaneous Math | 8 | 2008-11-09 07:45 |