![]() |
![]() |
#1 |
"Forget I exist"
Jul 2009
Dumbassville
203008 Posts |
![]()
including examples in PARI/gp 2.8.0 we have:
Code:
lucas(p) = { my(u = 4, q = 1<<p - 1); for(k=3, p, u = (sqr(u)-2) % q); u == 0; } Code:
lucas1(p) = { my(u = 4, q = 1<<p - 1,k=2); while(k<p, u = (sqr(u)-2) % q;k++); u == 0; } Code:
lucas2(p) = { my(u = 4, q = 1<<p - 1,k=2); until(k==p, u = (sqr(u)-2) % q;k++); u == 0; } Code:
lucas3(p) = { my(u = 4, q = 1<<p - 1,k=2); until(k==p, u = (norm(u)-2) % q;k++); u == 0; } Code:
LL1(p,d)={ my(s=6,P=1<<p-1); for(x=1,p-1, s=(fromdigits(d,if(s%P>1,s%P,s))-1)%P ); s==0 } and Code:
LL2(p,d)={ my(s=6,P=1<<p-1); for(x=1,p-1, s=subst(Pol([2,4,0],y),y,if(s%P>1,s%P,s))%P ); s==0 } Code:
lucaslehmer(p) ={ s=Mod(4,2^p-1); for(x=1,p-2, s=s^2-2 ); if(s==0,1,0) } Last fiddled with by science_man_88 on 2015-12-31 at 13:27 |
![]() |
![]() |
![]() |
#2 |
"Robert Gerbicz"
Oct 2005
Hungary
31·47 Posts |
![]()
A short and faster example, still in PARI-GP:
Code:
mylucas(p)={mp=2^p-1; x=4;for(i=1,p-2,x=sqr(x); hi=shift(x,-p);lo=bitand(x,mp); x=lo+hi-2; if(x>=mp,x-=mp);if(x<0,x+=mp)); return(x==0)} |
![]() |
![]() |
![]() |
#3 |
"Forget I exist"
Jul 2009
Dumbassville
26·131 Posts |
![]()
wow that is fast ( like 3 times how fast lucas runs on my system) and potentially parallelizable although I don't see how bitand(x,mp) will be different than x unless x>mp maybe that could be coded in as a speedup. also your two separate if statements about x can use the switch case way of writing it.
|
![]() |
![]() |
![]() |
#4 |
"Dana Jacobsen"
Feb 2011
Bangkok, TH
16148 Posts |
![]()
The C+GMP example on RosettaCode shows quite a few basic pretests, and also speeding up the modulo (since it is a special form), which it looks like the admirably short example from Gerbicz does.
|
![]() |
![]() |
![]() |
#5 | |
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
![]() Quote:
Code:
parforprime(x=1,9941,c=mylucas(x);if(c,print(x))) Last fiddled with by science_man_88 on 2015-12-31 at 20:36 |
|
![]() |
![]() |
![]() |
#6 |
Romulan Interpreter
Jun 2011
Thailand
33×347 Posts |
![]() |
![]() |
![]() |
![]() |
#7 |
"Forget I exist"
Jul 2009
Dumbassville
838410 Posts |
![]()
I think you'll find that it depends on what version of PARI/gp and architecture example:
Code:
(23:25) gp > mylucas(p)={mp=1<<p-1; x=4;for(i=1,p-2,x=sqr(x); hi=shift(x,-p);lo=bitand(x,mp); x=lo+hi-2; if(x>=mp,x-=mp);if(x<0,x+=mp)); return(x==0)} %136 = (p)->mp=1<<p-1;x=4;for(i=1,p-2,x=sqr(x);hi=shift(x,-p);lo=bitand(x,mp);x=lo+hi-2;if(x>=mp,x-=mp);if(x<0,x+=mp));return(x==0) (23:38) gp > parforprime(x=1,9941,if(x%4==3 && isprime(x<<1+1),0,factor(1<<x-1,1<<min(x\2,12))[1,1]!=[;],0,mylucas(x)),c,if(c,print(x))) 5 7 13 17 19 31 61 89 107 127 521 607 1279 2203 2281 3217 4253 4423 9689 9941 (23:39) gp > ## *** last result computed in 30,161 ms. Code:
(23:39) gp > mylucas(p)=mp=2^p-1;x=4;for(i=1,p-2,x=sqr(x);hi=shift(x,-p);lo=bitand(x,mp);x=lo+hi-2;if(x>=mp,x-=mp);if(x<0,x+=mp));return(x==0) %138 = (p)->mp=2^p-1;x=4;for(i=1,p-2,x=sqr(x);hi=shift(x,-p);lo=bitand(x,mp);x=lo+hi-2;if(x>=mp,x-=mp);if(x<0,x+=mp));return(x==0) (23:39) gp > parforprime(x=1,9941,if(x%4==3 && isprime(x<<1+1),0,factor(1<<x-1,1<<min(x\2,12))[1,1]!=[;],0,mylucas(x)),c,if(c,print(x))) 5 7 13 17 19 31 61 89 107 127 521 607 1279 2203 2281 3217 4253 4423 9689 9941 (23:40) gp > ## *** last result computed in 30,023 ms. Last fiddled with by science_man_88 on 2016-01-01 at 03:51 |
![]() |
![]() |
![]() |
#8 | |
Jun 2003
132F16 Posts |
![]() Quote:
Code:
if(x<0,x+=mp) |
|
![]() |
![]() |
![]() |
#9 |
Bamboozled!
"πΊππ·π·π"
May 2003
Down not across
22×3×887 Posts |
![]() |
![]() |
![]() |
![]() |
#10 |
"Robert Gerbicz"
Oct 2005
Hungary
26618 Posts |
![]()
I'd like to give a shorter and cleaner code. And actually I don't see any speedup, tried 6 runs to get x=1<<100000000-1; and my version of y=2^100000000-1;
it is quite likely that pari is using shifts for the calculation of 2^n and no multiplication. Yes, it is true, but you need a proof that you can skip that. Last fiddled with by R. Gerbicz on 2016-01-01 at 09:50 |
![]() |
![]() |
![]() |
#11 | |
"Forget I exist"
Jul 2009
Dumbassville
26×131 Posts |
![]() Quote:
Last fiddled with by science_man_88 on 2016-01-01 at 13:19 |
|
![]() |
![]() |
![]() |
Thread Tools | |
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
The Joys of Cracked.com: 5 Ways We Ruined the Occupy Wall Street Generation | Dubslow | Soap Box | 17 | 2012-05-14 08:51 |
RDS's unique pedagogic ways | R.D. Silverman | Soap Box | 137 | 2012-01-07 07:52 |
ways to get rid of oil spills | science_man_88 | Puzzles | 9 | 2010-07-30 21:22 |
Help needed to test Athlon64 code | geoff | Programming | 7 | 2006-08-18 12:16 |
Creative ways to achieve Athlon 64 / Opteron optimization | GP2 | Hardware | 11 | 2004-01-21 03:01 |