mersenneforum.org  

Go Back   mersenneforum.org > Great Internet Mersenne Prime Search > Math

Reply
 
Thread Tools
Old 2018-09-28, 12:39   #12
Dr Sardonicus
 
Dr Sardonicus's Avatar
 
Feb 2017
Nowhere

10010001000112 Posts
Default If there's a simple formula, use it...

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)
Dr Sardonicus is offline   Reply With Quote
Old 2018-09-28, 13:20   #13
axn
 
axn's Avatar
 
Jun 2003

5,051 Posts
Default

Quote:
Originally Posted by Dr Sardonicus View Post
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)
Removing the vector stuff and just counting the hits, your code runs in 6ms while mine runs in 385 ms. So they're basically the same

Actually, if you replace ispseudoprime with isprime, your code takes a whopping 6ms
axn is offline   Reply With Quote
Old 2018-09-28, 13:41   #14
Dr Sardonicus
 
Dr Sardonicus's Avatar
 
Feb 2017
Nowhere

4,643 Posts
Default

Quote:
Originally Posted by axn View Post
Removing the vector stuff and just counting the hits, your code runs in 6ms while mine runs in 385 ms. So they're basically the same

Actually, if you replace ispseudoprime with isprime, your code takes a whopping 6ms
Although my computer is way slower, using isprime() instead of ispseudprime() still did not increase the runtime in ms on my machine. I didn't run the "plow through all the primes from 10^6 to 10^7" method. It's good to have a point of reference for the runtimes.

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.
Dr Sardonicus is offline   Reply With Quote
Old 2018-09-28, 15:48   #15
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by Dr Sardonicus View Post
Although my computer is way slower, using isprime() instead of ispseudprime() still did not increase the runtime in ms on my machine.
I had Karim call ispseudoprime for all numbers below 2^64 since there are no BPSW pseudoprimes below that limit, so there's no runtime difference.
CRGreathouse is offline   Reply With Quote
Old 2018-09-28, 16:03   #16
ET_
Banned
 
ET_'s Avatar
 
"Luigi"
Aug 2002
Team Italia

32×5×107 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
I had Karim call ispseudoprime for all numbers below 2^64 since there are no BPSW pseudoprimes below that limit, so there's no runtime difference.
ET_ is offline   Reply With Quote
Old 2018-09-28, 16:11   #17
danaj
 
"Dana Jacobsen"
Feb 2011
Bangkok, TH

22×227 Posts
Default

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
danaj is offline   Reply With Quote
Reply



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

All times are UTC. The time now is 18:45.


Fri Jul 16 18:45:25 UTC 2021 up 49 days, 16:32, 1 user, load averages: 4.69, 5.25, 4.80

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.