[This article was first published on Tony's bubble universe » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
The 16th problem is another big-number problem:
215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000?
This is related to the precision of calculation. Although 2^1000 is within the numeric limit of R, the precision is limited with the digits number up to 22. Thus, I’m using gmp() package as previously mentioned. “gmp” stands for GNU Multiple Precision Arithmetic. It is a R package for arithmetic calculation without limitations (download here). With its help, to work with big numbers will be as easy as pie.
1 2 3 4 5 6 | library(gmp) num <- pow.bigz(2, 1000) digits <- strsplit(as.character(num), split = "") result <- sum(as.numeric(unlist(digits))) cat("The result is:", result, "\n") |
To leave a comment for the author, please follow the link and comment on their blog: Tony's bubble universe » R.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.