mersenneforum.org  

Go Back   mersenneforum.org > Math Stuff > Computer Science & Computational Number Theory > PARI/GP

Reply
 
Thread Tools
Old 2010-11-24, 15:03   #1673
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
Also, what is sumdigits? A010888? A007953? Something else?
sumdigits is a function in the thread already pointed back to by Pi's questioning.
science_man_88 is offline   Reply With Quote
Old 2010-11-24, 15:35   #1674
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

3×1,993 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
sumdigits is a function in the thread already pointed back to by Pi's questioning.
So, neither. It's like the digital root but with sumdigits(0) = 9.

Faster code:
Code:
sumdigits(n)=n=n%9;if(n,n,9)
CRGreathouse is offline   Reply With Quote
Old 2010-11-24, 16:13   #1675
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
So, neither. It's like the digital root but with sumdigits(0) = 9.

Faster code:
Code:
sumdigits(n)=n=n%9;if(n,n,9)
all i have to do is take out the return() on each to cut the time by 66% or so. and yours didn't beat mine by much on my machine ( I'll double check with multiple runs)
science_man_88 is offline   Reply With Quote
Old 2010-11-24, 16:17   #1676
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Code:
(12:13) gp > sumdigits1(n)=if(n%9!=0,n%9,9)
%5 = (n)->if(n%9!=0,n%9,9)
(12:15) gp > sumdigits2(n)=if(n%9!=0,return(n%9),return(9))
%6 = (n)->if(n%9!=0,return(n%9),return(9))
(12:15) gp > sumdigits3(n)=n=n%9;if(n,n,9)
%7 = (n)->n=n%9;if(n,n,9)
(12:15) gp > for(x=1,10000000,sumdigits1(x))
(12:16) gp > ##
  ***   last result computed in 6,172 ms.
(12:16) gp > for(x=1,10000000,sumdigits2(x))
(12:16) gp > ##
  ***   last result computed in 16,563 ms.
(12:16) gp > for(x=1,10000000,sumdigits3(x))
(12:17) gp > ##
  ***   last result computed in 15,141 ms.
unfortunately my first sumdigits seems most effective when added into lucaslehmers()

Last fiddled with by science_man_88 on 2010-11-24 at 17:14
science_man_88 is offline   Reply With Quote
Old 2010-11-24, 18:41   #1677
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

135338 Posts
Default

Interesting. I wonder what Pari is doing under the hood here.

Mine is still faster for large numbers, but I can't imagine why yours is faster for small numbers.

The optimal Pari (as opposed to GP) code is surely
Code:
long
digital_root(GEN n) {
  if (typ(n) != t_INT)
    pari_err(typeer, "digital_root");
  register long ret = smodis(n, 9);
  if (ret)
    return ret;
  return 9;
}
and I can't guess why your version would be closer to this than mine. Maybe I'll gp2c them later and find out.

Last fiddled with by CRGreathouse on 2010-11-24 at 18:42
CRGreathouse is offline   Reply With Quote
Old 2010-11-24, 18:42   #1678
CRGreathouse
 
CRGreathouse's Avatar
 
Aug 2006

135338 Posts
Default

Quote:
Originally Posted by science_man_88 View Post
unfortunately my first sumdigits seems most effective when added into lucaslehmers()
Do you need the Lucas-Lehmer residue, or just its digital root? You can compute the latter much faster.
CRGreathouse is offline   Reply With Quote
Old 2010-11-24, 19:10   #1679
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

100000110000002 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
Do you need the Lucas-Lehmer residue, or just its digital root? You can compute the latter much faster.
Digital root of the lucas lehmer residue ... really I didn't know that, Thanks. I'm just trying to limit it to a list of digital root 9 because we know the exponents must be in that group to work.

Last fiddled with by science_man_88 on 2010-11-24 at 19:11
science_man_88 is offline   Reply With Quote
Old 2010-11-24, 19:45   #1680
axn
 
axn's Avatar
 
Jun 2003

5,087 Posts
Default

Quote:
Originally Posted by CRGreathouse View Post
So, neither. It's like the digital root but with sumdigits(0) = 9.

Faster code:
Code:
sumdigits(n)=n=n%9;if(n,n,9)
what about
Code:
sumdigits(n)=(n-1)%9+1
?
axn is offline   Reply With Quote
Old 2010-11-24, 19:59   #1681
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

203008 Posts
Default

Quote:
Originally Posted by axn View Post
what about
Code:
sumdigits(n)=(n-1)%9+1
?
using flat out print(sumdigits(x)) it ties my fastest on my machine. I'll have to check it out on the lucas test.

Last fiddled with by science_man_88 on 2010-11-24 at 20:02
science_man_88 is offline   Reply With Quote
Old 2010-11-24, 20:03   #1682
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Code:
(15:57) gp > for(x=1,10000000,sumdigits1(x))
(16:01) gp > ##
  ***   last result computed in 6,172 ms.
(16:01) gp > for(x=1,10000000,sumdigits2(x))
(16:01) gp > ##
  ***   last result computed in 16,891 ms.
(16:01) gp > for(x=1,10000000,sumdigits3(x))
(16:01) gp > ##
  ***   last result computed in 15,312 ms.
(16:01) gp > for(x=1,10000000,sumdigits4(x))
(16:02) gp > ##
  ***   last result computed in 5,266 ms.
(16:02) gp >
we have a new winner.
science_man_88 is offline   Reply With Quote
Old 2010-11-24, 20:06   #1683
science_man_88
 
science_man_88's Avatar
 
"Forget I exist"
Jul 2009
Dumbassville

26×131 Posts
Default

Code:
(16:04) gp > for(x=1,100000,lucaslehmer2(x))
3,5,7,13,17,19,23,31,33,51,61,71,89,101,107,127,139,191,271,273,305,331,347,351,367,397,405,407
  *** _^s: user interrupt after 12,562 ms.
(16:04) gp > for(x=1,1000,lucaslehmer2(x))
3,5,7,13,17,19,23,31,33,51,61,71,89,101,107,127,139,191,271,273,305,331,347,351,367,397,405,407
(16:04) gp > ##
  ***   last result computed in 3,922 ms.
(16:04) gp > lucaslehmer2(p)= s=4;for(x=1,p-2,s=(s^2-2)%(2^p-1));if(x=p-2 && sumdigits2(s)==9,p
%21 = (p)->s=4;for(x=1,p-2,s=(s^2-2)%(2^p-1));if(x=p-2&&sumdigits2(s)==9,print1(p","))
(16:05) gp > for(x=1,1000,lucaslehmer2(x))
3,5,7,13,17,19,23,31,33,51,61,71,89,101,107,127,139,191,271,273,305,331,347,351,367,397,405,407
(16:05) gp > ##
  ***   last result computed in 3,953 ms.
(16:05) gp > lucaslehmer2(p)= s=4;for(x=1,p-2,s=(s^2-2)%(2^p-1));if(x=p-2 && sumdigits3(s)==9,p
%22 = (p)->s=4;for(x=1,p-2,s=(s^2-2)%(2^p-1));if(x=p-2&&sumdigits3(s)==9,print1(p","))
(16:05) gp > for(x=1,1000,lucaslehmer2(x))
3,5,7,13,17,19,23,31,33,51,61,71,89,101,107,127,139,191,271,273,305,331,347,351,367,397,405,407
(16:05) gp > ##
  ***   last result computed in 3,969 ms.
(16:05) gp > lucaslehmer2(p)= s=4;for(x=1,p-2,s=(s^2-2)%(2^p-1));if(x=p-2 && sumdigits4(s)==9,p
%23 = (p)->s=4;for(x=1,p-2,s=(s^2-2)%(2^p-1));if(x=p-2&&sumdigits4(s)==9,print1(p","))
(16:05) gp > for(x=1,1000,lucaslehmer2(x))
3,5,7,13,17,19,23,31,33,51,61,71,89,101,107,127,139,191,271,273,305,331,347,351,367,397,405,407
(16:05) gp > ##
  ***   last result computed in 4,000 ms.
(16:05) gp >
this is weirder and weirder lol.

Last fiddled with by science_man_88 on 2010-11-24 at 20:07
science_man_88 is offline   Reply With Quote
Reply

Thread Tools


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why do I sometimes see all the <> formatting commands when I quote or edit? cheesehead Forum Feedback 3 2013-05-25 12:56
Passing commands to PARI on Windows James Heinrich Software 2 2012-05-13 19:19
Ubiquity commands Mini-Geek Aliquot Sequences 1 2009-09-22 19:33
64-bit Pari? CRGreathouse Software 2 2009-03-13 04:22
Are these commands correct? jasong Linux 2 2007-10-18 23:40

All times are UTC. The time now is 23:06.


Fri Aug 6 23:06:58 UTC 2021 up 14 days, 17:35, 1 user, load averages: 4.21, 3.93, 3.92

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.