<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/euler-problem-16-power-digit-sum/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/euler-problem-16-power-digit-sum/"> The Devil is in the Data</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="" data-orig-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/Chessboard-Theory.jpg?fit=400%2C222" data-orig-size="400,222" data-image-title="Chessboard Theory" class="wp-image-223" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="223" title="Euler Problem 16: Power Digit Sum" data-permalink="http://r.prevos.net/euler-problem-16-power-digit-sum/chessboard-theory/" alt="Euler Problem 16: Power Digit Sum" data-medium-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/Chessboard-Theory.jpg?fit=300%2C167" data-large-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/Chessboard-Theory.jpg?fit=400%2C222" data-image-meta="{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}" src="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/03/Chessboard-Theory.jpg?resize=300%2C167">Euler Problem 16 is reminiscent of the famous <a target="_blank" rel="nofollow" href="http://trailblazernetizens.blogspot.com.au/2015/08/the-amazing-chessboard-theory.html">fable of wheat and chess</a>. Lahur Sessa invented the game of chess for King Iadava. The king was very pleased with the game and asked Lahur to name his reward.</p>
<p>Lahur asked the king to place one grain of rice on the first square of a chessboard, two on the next square, four on the third square and so on until the board is filled. The king was happy with his humble request until his mathematicians worked out that it would take millions of tonnes of grain. Assuming there are <a target="_blank" rel="nofollow" href="https://www.reference.com/food/many-grains-wheat-gram-88a4bf0a5a8ebff3">25 grains of wheat in a gramme</a>, the last field will contain more than 461,168,602,000 tonnes of grain.</p>
<h2>Euler Problem 16 Definition</h2>
<p><img title="2^{15} = 32768 " alt="2^{15} = 32768 " class="latex" src="http://s0.wp.com/latex.php?latex=2%5E%7B15%7D+%3D+32768+&bg=ffffff&%23038;fg=000&%23038;s=0"> and the sum of its digits is <img title="3 + 2 + 7 + 6 + 8 = 26 " alt="3 + 2 + 7 + 6 + 8 = 26 " class="latex" src="http://s0.wp.com/latex.php?latex=3+%2B+2+%2B+7+%2B+6+%2B+8+%3D+26+&bg=ffffff&%23038;fg=000&%23038;s=0">. What is the sum of the digits of the number <img title="2^{1000} " alt="2^{1000} " class="latex" src="http://s0.wp.com/latex.php?latex=2%5E%7B1000%7D+&bg=ffffff&%23038;fg=000&%23038;s=0">?</p>
<h2>Solution</h2>
<p>The most straightforward solution uses the <a target="_blank" rel="nofollow" href="https://cran.r-project.org/web/packages/gmp/index.html">GMP</a> package for Multiple Precision Arithmetic to calculate big integers. The as.bigz function results in a special class of arbitrarily large integer numbers</p>
<pre>
library(gmp)
digits &lt;- as.bigz(2^1000) # Define number
answer &lt;- sum(as.numeric(unlist(strsplit(as.character(digits), &quot;&quot;))))
</pre>
<p>We can also solve this problem in base-r with the bigg<em>.add</em> function which I developed for <a target="_blank" rel="nofollow" href="http://r.prevos.net/euler-problem-13/">Euler Problem 13</a>. This function uses basic string operations to add to arbitrarily large numbers. Raising a number to the power of two can also be written as a series of additions:</p>
<p><img title="2^4 = 2 \times 2 \times 2 \times 2 = ((2+2)+(2+2)) + ((2+2)+(2+2)) " alt="2^4 = 2 \times 2 \times 2 \times 2 = ((2+2)+(2+2)) + ((2+2)+(2+2)) " class="latex" src="http://s0.wp.com/latex.php?latex=2%5E4+%3D+2+%5Ctimes+2+%5Ctimes+2+%5Ctimes+2+%3D+%28%282%2B2%29%2B%282%2B2%29%29+%2B%C2%A0%28%282%2B2%29%2B%282%2B2%29%29+&bg=ffffff&%23038;fg=000&%23038;s=1"></p>
<p>The solution to this problem is to add 2 + 2 then add the outcome of that equation to itself, and so on. Repeat this one thousand times to raise the number two to the power of one thousand.</p>
<pre>
pow &lt;- 2
for (i in 2:1000)
    pow &lt;- big.add(pow, pow)
answer &lt;- sum(as.numeric(unlist(strsplit(pow, &quot;&quot;))))
</pre>
<p>The post <a target="_blank" rel="nofollow" href="http://r.prevos.net/euler-problem-16-power-digit-sum/">Euler Problem 16: Power Digit Sum</a> appeared first on <a target="_blank" rel="nofollow" href="http://r.prevos.net/">The Devil is in the Data</a>.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://r.prevos.net/euler-problem-16-power-digit-sum/"> The Devil is in the Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/qE9RJz6tpwY"></a></div>