[R] Arbitrary Precision Numbers

Earl F. Glynn efg at stowers-institute.org
Thu Apr 10 17:09:21 CEST 2008


"Rory Winston" <rory.winston at gmail.com> wrote in message 
news:16492549.post at 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



More information about the R-help mailing list