Project Euler — problem 16

July 21, 2012

(This article was first published on Tony's bubble universe » R, and kindly contributed to R-bloggers)

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.

?View Code RSPLUS

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. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: , ,

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)