[R] Arbitrary Precision Numbers

Rory Winston rory.winston at gmail.com
Thu Apr 10 21:20:15 CEST 2008


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 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
> 
> ______________________________________________
> R-help at 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/Arbitrary-Precision-Numbers-tp16492549p16615201.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list