![]() |
|
|
#45 | ||
|
Oct 2007
Manchester, UK
101010010112 Posts |
Quote:
R. Gerbicz posted some good code, he used a forprime loop to generate all primes for the last 8 digits, and only incremented a counter if p%10^4 was also prime. This completely removes checking the primality of lots of 8 digit numbers from the code. Modified, you could increment a counter if it passes the checksum, and the full 16 digit number is prime too. But it will still take a REALLY long time. Sieving really does seem to be the way to go. Quote:
|
||
|
|
|
|
|
#46 |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
you realize 2^32 has 10 digits and that's above what my primelimit can goto so no I don't need a forprime loop unless I'm checking ones under 2^32.
Last fiddled with by science_man_88 on 2011-01-21 at 22:49 |
|
|
|
|
|
#47 |
|
Oct 2007
Manchester, UK
5×271 Posts |
Your loop increments 2 every time, this means that you must run 10^16 / 2 = 5*10^15 iterations. If instead you had a forprime loop running from 2 to 10^8, and a regular for loop nested inside running from 0 to 10^8, the total number of iterations inside both loops would be 5.76*10^14.
However, you can cut that number down more by performing the primality check on the last 4 digits before entering the inside loop. This drops the number of iterations inside both loops to 1.77*10^14. It also removes the need for nearly 10^16 primality tests. |
|
|
|
|
|
#48 | |
|
"Forget I exist"
Jul 2009
Dumbassville
26·131 Posts |
Quote:
Code:
(18:51)>b=0;forstep(x=1000000000000001,1000000100000001,2,if(isprime(x%(10^4)) && isprime(x%(10^8)) && isprime(x) && luhn(x),b=b+1));print(b) 8959 (18:57)>## *** last result computed in 4mn, 44,641 ms. |
|
|
|
|
|
|
#49 | |
|
"Forget I exist"
Jul 2009
Dumbassville
100000110000002 Posts |
Quote:
Last fiddled with by science_man_88 on 2011-01-21 at 23:10 |
|
|
|
|
|
|
#50 |
|
"Forget I exist"
Jul 2009
Dumbassville
100000110000002 Posts |
never mind you beat me lol, thanks for the tip I can tell you a small list lol.
Code:
(19:18)>b=0;forprime(x=1,10^8-1,if(isprime(x%(10^4)),for(y=1,10^8-1,if(isprime(10^8*y+x) && luhn(10^8*y+x),print(10^8*y+x))))) list cleaned for safety purposes. *** print: user interrupt after 52,672 ms. Last fiddled with by science_man_88 on 2011-01-21 at 23:57 |
|
|
|
|
|
#51 |
|
Oct 2007
Manchester, UK
5·271 Posts |
I would be interested to see your luhn function. I wrote my own luhn function, and ran your code using it. I got a result of 8464, whereas you posted 8959. I wrote some double-loop code and that also returned 8464. The difference lies in the luhn function.
There are the timings I recorded: Your code: 6mn, 19,644 ms My code: 5mn, 27,758 ms (16% faster) Then I reversed the order of the isprime(<16digitnum>) and luhn functions, and got these timings: Your code: 1mn, 49,278 ms My code: 57,409 ms (90% faster) For a real demonstration of how much quicker the double loop method is, I removed the isprime(<16digitnum>) and luhn checks: Your code: 49,795 ms My code: 5,647 ms (782% faster) I have no idea how you recorded a time of 4mn, 44,641 ms for that run a couple of posts up, I ran the same code but without the luhn check (so it should have been faster) and it took 6mn, 15,573 ms. This was on a 4 GHz dedicated CPU core. Do you have an overclocked Sandy Bridge? Anyway, onto actual code. Here is my luhn function and double-loop code: Code:
{
(l(x)=
t=2*floor(x%100000000/10000000)+(x%100000000>49999999)+floor(x%10000000/1000000)+2*floor(x%1000000/100000)+(x%1000000>499999)+floor(x%100000/10000)+2*floor(x%10000/1000)+(x%10000>4999)+floor(x%1000/100)+2*floor(x%100/10)+(x%100>49)+x%10;
x=(x-x%100000000)/100000000;
return((t+2*floor(x%100000000/10000000)+(x%100000000>49999999)+floor(x%10000000/1000000)+2*floor(x%1000000/100000)+(x%1000000>499999)+floor(x%100000/10000)+2*floor(x%10000/1000)+(x%10000>4999)+floor(x%1000/100)+2*floor(x%100/10)+(x%100>49)+x%10)%10==0)
);
n=0;
forprime(p=3,100000000,
if(isprime(p%10000),
for(a=10000000,10000000,
if(l(a*100000000+p)&&isprime(a*100000000+p),n++)
)
)
);
print(n);
}
Edit: Despite being made several hours ago, I didn't actually see your last post, but upon reading it now I see that we posted virtually identical code. I would still like to see your luhn function though. Last fiddled with by lavalamp on 2011-01-22 at 03:20 |
|
|
|
|
|
#52 | |
|
"Forget I exist"
Jul 2009
Dumbassville
26·131 Posts |
Quote:
I'll have to recreate my luhn code I involved my sumdigits code for the 4 min and something test I believe. Code:
(x)->w=eval(Vec(Str(x)));a=0;if(#w%2==1,for(i=1,#w,if(i%2==0,a=a+(2*w[i]),a=a+w[i])),for(i=1,#w,if(i%2==1,a=a+(2*w[i]),a=a+w[i])));if(a%10==0,return(1),return(0)) Last fiddled with by science_man_88 on 2011-01-22 at 12:45 |
|
|
|
|
|
|
#53 | |
|
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
Quote:
|
|
|
|
|
|
|
#54 |
|
"Richard B. Woods"
Aug 2002
Wisconsin USA
22×3×641 Posts |
Speaking of the number of combinations of digits that follow certain rules:
Yesterday I received my copy of Knuth 4A (or "The Art of Computer Programming, Volume 4A, Combinatorial Algorithms, Part 1" by Donald E. Knuth). |
|
|
|
|
|
#55 |
|
May 2004
New York City
102128 Posts |
I may have to look in the stores for it.
|
|
|
|
![]() |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Aouessare-El Haddouchi-Essaaidi "test": "if Mp has no factor, it is prime!" | wildrabbitt | Miscellaneous Math | 11 | 2015-03-06 08:17 |
| AMD Announces Industry's First "Supercomputing" Server Graphics Card | ET_ | GPU Computing | 23 | 2013-11-18 17:49 |
| Speeding up double checking when first test returns "prime" | Unregistered | PrimeNet | 16 | 2006-02-28 02:00 |
| Oh noes! The "42nd Mersenne prime" isn't prime! | ixfd64 | Lounge | 7 | 2005-04-03 19:27 |
| Would Minimizing "iterations between results file" may reveal "is not prime" earlier? | nitai1999 | Software | 7 | 2004-08-26 18:12 |