Arbitrary Precision Numbers

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Arbitrary Precision Numbers

Rory Winston
Hi

(If you're wondering, this is a Project Euler question :))

If I wanted to calculate the sum of the digits in the decimal representation of 2^1000, what would be a good way to go about that? I've tried the following methods:

# Calculate the sum of digits in the decimal representation of 2^n
# Only works for smaller values of n
bsum <- function(n) {
        s <- 0
        e <- floor(log10(2^n))
        for (i in seq(e,0,-1)) {
                s <- s + (((2^n) %/% 10^i) %% 10)
        }
        s
}

The above doesnt work, as I am using an integer modulus which has insufficient precision. so I tried to coerce R to produce a full-precision integer and sum the digits of that:

bsum2 <- function(n) {
        s <- 0
        strn <- formatC(2^n, format="fg")
        sum(as.numeric(strsplit(strn,"")[[1]]))
}

This also doesnt seem to work. Is there another way I can do this in R?
Cheers
Rory
Reply | Threaded
Open this post in threaded view
|

Re: Arbitrary Precision Numbers

jholtman
One way of "doing it in R" is to use the 'bc' command that is on most
UNIX/LINUX systems and is also on cygwin for Windows.  Here are
decimal digits for 2^1000

/cygdrive/c/jph/consulting/2008-03-30: bc
bc 1.06
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
2^1000
10715086071862673209484250490600018105614048117055336074437503883703\
51051124936122493198378815695858127594672917553146825187145285692314\
04359845775746985748039345677748242309854210746050623711418779541821\
53046474983581941267398767559165543946077062914571196477686542167660\
429831652624386837205668069376

So you can 'shell' the command from R, collect the digits and then sum them.



On Fri, Apr 4, 2008 at 10:43 AM, Rory Winston <[hidden email]> wrote:

>
> Hi
>
> (If you're wondering, this is a Project Euler question :))
>
> If I wanted to calculate the sum of the digits in the decimal representation
> of 2^1000, what would be a good way to go about that? I've tried the
> following methods:
>
> # Calculate the sum of digits in the decimal representation of 2^n
> # Only works for smaller values of n
> bsum <- function(n) {
>        s <- 0
>        e <- floor(log10(2^n))
>        for (i in seq(e,0,-1)) {
>                s <- s + (((2^n) %/% 10^i) %% 10)
>        }
>        s
> }
>
> The above doesnt work, as I am using an integer modulus which has
> insufficient precision. so I tried to coerce R to produce a full-precision
> integer and sum the digits of that:
>
> bsum2 <- function(n) {
>        s <- 0
>        strn <- formatC(2^n, format="fg")
>        sum(as.numeric(strsplit(strn,"")[[1]]))
> }
>
> This also doesnt seem to work. Is there another way I can do this in R?
> Cheers
> Rory
> --
> View this message in context: http://www.nabble.com/Arbitrary-Precision-Numbers-tp16492549p16492549.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



--
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Arbitrary Precision Numbers

Gabor Grothendieck
In reply to this post by Rory Winston
Have a look at the Ryacas package which interfaces R to yacas.
yacas can do exact arithmetic.

On Fri, Apr 4, 2008 at 11:43 AM, Rory Winston <[hidden email]> wrote:

>
> Hi
>
> (If you're wondering, this is a Project Euler question :))
>
> If I wanted to calculate the sum of the digits in the decimal representation
> of 2^1000, what would be a good way to go about that? I've tried the
> following methods:
>
> # Calculate the sum of digits in the decimal representation of 2^n
> # Only works for smaller values of n
> bsum <- function(n) {
>        s <- 0
>        e <- floor(log10(2^n))
>        for (i in seq(e,0,-1)) {
>                s <- s + (((2^n) %/% 10^i) %% 10)
>        }
>        s
> }
>
> The above doesnt work, as I am using an integer modulus which has
> insufficient precision. so I tried to coerce R to produce a full-precision
> integer and sum the digits of that:
>
> bsum2 <- function(n) {
>        s <- 0
>        strn <- formatC(2^n, format="fg")
>        sum(as.numeric(strsplit(strn,"")[[1]]))
> }
>
> This also doesnt seem to work. Is there another way I can do this in R?
> Cheers
> Rory
> --
> View this message in context: http://www.nabble.com/Arbitrary-Precision-Numbers-tp16492549p16492549.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Arbitrary Precision Numbers

Gabor Grothendieck
Here is the link:
http://ryacas.googlecode.om

On Fri, Apr 4, 2008 at 9:55 PM, Gabor Grothendieck
<[hidden email]> wrote:

> Have a look at the Ryacas package which interfaces R to yacas.
> yacas can do exact arithmetic.
>
>
> On Fri, Apr 4, 2008 at 11:43 AM, Rory Winston <[hidden email]> wrote:
> >
> > Hi
> >
> > (If you're wondering, this is a Project Euler question :))
> >
> > If I wanted to calculate the sum of the digits in the decimal representation
> > of 2^1000, what would be a good way to go about that? I've tried the
> > following methods:
> >
> > # Calculate the sum of digits in the decimal representation of 2^n
> > # Only works for smaller values of n
> > bsum <- function(n) {
> >        s <- 0
> >        e <- floor(log10(2^n))
> >        for (i in seq(e,0,-1)) {
> >                s <- s + (((2^n) %/% 10^i) %% 10)
> >        }
> >        s
> > }
> >
> > The above doesnt work, as I am using an integer modulus which has
> > insufficient precision. so I tried to coerce R to produce a full-precision
> > integer and sum the digits of that:
> >
> > bsum2 <- function(n) {
> >        s <- 0
> >        strn <- formatC(2^n, format="fg")
> >        sum(as.numeric(strsplit(strn,"")[[1]]))
> > }
> >
> > This also doesnt seem to work. Is there another way I can do this in R?
> > Cheers
> > Rory
> > --
> > View this message in context: http://www.nabble.com/Arbitrary-Precision-Numbers-tp16492549p16492549.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Arbitrary Precision Numbers

Charles Geyer
In reply to this post by Rory Winston
Probably not helpful for your particular problem, but the package rcdd
also has a fairly limited interface to the GNU MP Bignum library.
It does arithmetic on arbitrary precision rationals.  Also exact linear
programming and computational geometry using arbitrary precision rationals.

    library(rcdd)
    ?ArithmeticGMP
--
Charles Geyer
Professor, School of Statistics
University of Minnesota
[hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Arbitrary Precision Numbers

Earl F. Glynn
In reply to this post by Rory Winston
"Rory Winston" <[hidden email]> wrote in message
news:[hidden email]...
> (If you're wondering, this is a Project Euler question :))
>
> If I wanted to calculate the sum of the digits in the decimal
> representation
> of 2^1000, what would be a good way to go about that?

Try this:

> library(gmp)
> for (N in c(10,16,32,100,1000))
+ {
+   s <- as.character(pow.bigz(2,N))
+   t <- as.numeric(unlist(strsplit(s,"")))
+   cat(N, s, sum(t), "\n")
+ }
10 1024 7
16 65536 25
32 4294967296 58
100 1267650600228229401496703205376 115
1000
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
1366

The first few can be verified manually.

--
efg

Earl F. Glynn
Bioinformatics
Stowers Institute for Medical Research

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Arbitrary Precision Numbers

Rory Winston
Thanks Earl

Thats exactly what I was looking for - an extension that uses libgmp and provides a bignum type that can be combined with standard operators and numeric variables. Somehow my original search on CRAN missed this one.

Cheers
Rory
Earl F. Glynn wrote
"Rory Winston" <rory.winston@gmail.com> wrote in message
news:16492549.post@talk.nabble.com...
> (If you're wondering, this is a Project Euler question :))
>
> If I wanted to calculate the sum of the digits in the decimal
> representation
> of 2^1000, what would be a good way to go about that?

Try this:

> library(gmp)
> for (N in c(10,16,32,100,1000))
+ {
+   s <- as.character(pow.bigz(2,N))
+   t <- as.numeric(unlist(strsplit(s,"")))
+   cat(N, s, sum(t), "\n")
+ }
10 1024 7
16 65536 25
32 4294967296 58
100 1267650600228229401496703205376 115
1000
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
1366

The first few can be verified manually.

--
efg

Earl F. Glynn
Bioinformatics
Stowers Institute for Medical Research

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.