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")