mersenneforum.org  

Go Back   mersenneforum.org > Fun Stuff > Puzzles

Reply
 
Thread Tools
Old 2011-01-21, 22:44   #45
lavalamp
 
lavalamp's Avatar
 
Oct 2007
Manchester, UK

101010010112 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
Code:
(17:19)>b=0;forstep(x=1000000000000001,1000000000100001,2,if(isprime(x) && isprime(x%10^8) && isprime(x%10^4) && luhn(x),b=b+1));print(b)
22
(17:20)>##
  ***   last result computed in 5,000 ms.
(17:21)>b=0;forstep(x=1000000000000001,1000000001000001,2,if(isprime(x) && isprime(x%10^8) && isprime(x%10^4) && luhn(x),b=b+1));print(b)
126
(17:22)>##
  ***   last result computed in 51,875 ms.
(17:22)>b=0;forstep(x=1000000000000001,1000000010000001,2,if(isprime(x) && isprime(x%10^8) && isprime(x%10^4) && luhn(x),b=b+1));print(b)
1019
(17:32)>##
  ***   last result computed in 8mn, 48,719 ms.
what does this tell you ?
That it will take you a LONG time to finish. Also that you should be using a forprime loop.

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:
Originally Posted by ewmayer View Post
This opens up another interesting avenue for winnowing candidates: Let's assume for the sake of argument that for the mystery 16-digit number N, I checked the primality of (N mod (10^d)) for d = 1,2,3,...,16, and those d-values I reported as yielding a prime are the only ones. (That's not what I did for my card number, but ... I could be lying. ;) How much would knowledge of the prime/not-prime status of all 16 power-of-10 residues narrow the list of possibilities?
Well, applying that to just the last 8 digits takes it from 1,767,355 candidates to 118443.
lavalamp is offline   Reply With Quote
Old 2011-01-21, 22:48   #46
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by lavalamp View Post
. Also that you should be using a forprime loop.
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
science_man_88 is offline   Reply With Quote
Old 2011-01-21, 23:01   #47
lavalamp
 
lavalamp's Avatar
 
Oct 2007
Manchester, UK

5×271 Posts
Default

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.
lavalamp is offline   Reply With Quote
Old 2011-01-21, 23:02   #48
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
You're going to 10^15 and it took you 8:48.719 to get to 10^7. Your expected completion time is (10^15 / 10^7) * 8:48.719 or 1675 years. Get comfy.
I tried reversing the isprime and it ended up being about half as long for a 1 digit increase in digits between that second and last 1. how much faster is that ?

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.
science_man_88 is offline   Reply With Quote
Old 2011-01-21, 23:09   #49
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

100000110000002 Posts
Default

Quote:
Originally Posted by lavalamp View Post
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.
it would increase the multiplications needed to get a result as the only way i see this working is by appending the 2 numbers on top of each other which means I'd have to multiply the last loop control variable by 10^8, to get the full 16 digit number to test. oh and to CRG my test last i checked were at minimal memory for PARI.

Last fiddled with by science_man_88 on 2011-01-21 at 23:10
science_man_88 is offline   Reply With Quote
Old 2011-01-21, 23:21   #50
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

100000110000002 Posts
Default

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.
431 in the list I cleared by the way. highest one was 13 digits i think.

Last fiddled with by science_man_88 on 2011-01-21 at 23:57
science_man_88 is offline   Reply With Quote
Old 2011-01-22, 03:16   #51
lavalamp
 
lavalamp's Avatar
 
Oct 2007
Manchester, UK

5·271 Posts
Default

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);

}
My luhn function (just l(x) in my code) looks kind of a mess, that's because I did some loop unrolling on it to make it faster. On my system it can do about 100,000 checks / second (10 µs each) for 16 digit numbers. The inner loop only runs once, that's so it runs over the same range as your code for a direct comparison.

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
lavalamp is offline   Reply With Quote
Old 2011-01-22, 12:20   #52
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26·131 Posts
Default

Quote:
Originally Posted by lavalamp View Post
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);

}
My luhn function (just l(x) in my code) looks kind of a mess, that's because I did some loop unrolling on it to make it faster. On my system it can do about 100,000 checks / second (10 µs each) for 16 digit numbers. The inner loop only runs once, that's so it runs over the same range as your code for a direct comparison.

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.
my computer reset and it's not dedicated or sandy bridge as far as I know.

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))
I've seen a problem with my code though and that's that in odd number of digits cases it would ad double the last digit(aka the sumdigit) I'll have to fix that. mind you I check if it's odd or even length, for using it for an even numbered case I should just use the second half and save time.

Last fiddled with by science_man_88 on 2011-01-22 at 12:45
science_man_88 is offline   Reply With Quote
Old 2011-01-22, 14:03   #53
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
I've seen a problem with my code though and that's that in odd number of digits cases it would ad double the last digit(aka the sumdigit) I'll have to fix that. mind you I check if it's odd or even length, for using it for an even numbered case I should just use the second half and save time.
god I'm stupid I've made sure it can't because the #w is odd in a odd length one but I'm only targeting even ones for doubling doh.
science_man_88 is offline   Reply With Quote
Old 2011-01-22, 22:19   #54
cheesehead
 
cheesehead's Avatar
 
"Richard B. Woods"
Aug 2002
Wisconsin USA

22×3×641 Posts
Default

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).
cheesehead is offline   Reply With Quote
Old 2011-01-22, 22:56   #55
davar55
 
davar55's Avatar
 
May 2004
New York City

102128 Posts
Default

Quote:
Originally Posted by cheesehead View Post
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).
I may have to look in the stores for it.
davar55 is offline   Reply With Quote
Reply



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

All times are UTC. The time now is 03:53.


Sat Jul 17 03:53:56 UTC 2021 up 50 days, 1:41, 1 user, load averages: 1.75, 1.87, 1.74

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.