<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/digit-fifth-powers-euler-problem-30/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://r.prevos.net/digit-fifth-powers-euler-problem-30/"> 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>Euler problem 30 is another number crunching problem that deals with numbers to the power of five. Two other Euler problems dealt with raising numbers to a power. The <a rel="nofollow" href="http://r.prevos.net/euler-problem-29-distinct-powers/" target="_blank">previous problem</a> looked at permutations of powers and <a rel="nofollow" href="http://r.prevos.net/euler-problem-16-power-digit-sum/" target="_blank">problem 16</a> asks for the sum of the digits of <img alt="2^{1000}" title="2^{1000}" src="https://s0.wp.com/latex.php?latex=2%5E%7B1000%7D&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex">.</p>
<p>Numberphile has a nice video about a trick to quickly calculate the fifth root of a number that makes you look like a mathematical wizard.</p>
<p><iframe allowfullscreen="true" src="https://www.youtube.com/embed/y8acoaakvPM?version=3&rel=1&%23038;fs=1&%23038;autohide=2&%23038;showsearch=0&%23038;showinfo=1&%23038;iv_load_policy=1&%23038;wmode=transparent" width="450" type="text/html"></iframe></p>
<h2>Euler Problem 30 Definition</h2>
<p>Surprisingly there are only three numbers that can be written as the sum of fourth powers of their digits:</p>
<p><img alt="1634 = 1^4 + 6^4 + 3^4 + 4^4" title="1634 = 1^4 + 6^4 + 3^4 + 4^4" src="https://s0.wp.com/latex.php?latex=1634+%3D+1%5E4+%2B+6%5E4+%2B+3%5E4+%2B+4%5E4&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"></p>
<p><img alt="8208 = 8^4 + 2^4 + 0^4 + 8^4" title="8208 = 8^4 + 2^4 + 0^4 + 8^4" src="https://s0.wp.com/latex.php?latex=8208+%3D+8%5E4+%2B+2%5E4+%2B+0%5E4+%2B+8%5E4&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"></p>
<p><img alt="9474 = 9^4 + 4^4 + 7^4 + 4^4" title="9474 = 9^4 + 4^4 + 7^4 + 4^4" src="https://s0.wp.com/latex.php?latex=9474+%3D+9%5E4+%2B+4%5E4+%2B+7%5E4+%2B+4%5E4&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"></p>
<p>As <img alt="1 = 1^4" title="1 = 1^4" src="https://s0.wp.com/latex.php?latex=1+%3D+1%5E4&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> is not a sum, it is not included.</p>
<p>The sum of these numbers is <img alt="1634 + 8208 + 9474 = 19316" title="1634 + 8208 + 9474 = 19316" src="https://s0.wp.com/latex.php?latex=1634+%2B+8208+%2B+9474+%3D+19316&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex">. <a rel="nofollow" href="https://projecteuler.net/problem=30" target="_blank">Find the sum of all the numbers that can be written as the sum of fifth powers of their digits</a>.</p>
<h2>Proposed Solution</h2>
<p>The problem asks for a brute-force solution but we have a halting problem. How far do we need to go before we can be certain there are no sums of fifth power digits? The highest digit is <img alt="9 " title="9 " src="https://s0.wp.com/latex.php?latex=9+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> and <img alt="9^5=59049" title="9^5=59049" src="https://s0.wp.com/latex.php?latex=9%5E5%3D59049&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex">, which has five digits. If we then look at <img alt="5 times 9^5=295245" title="5 times 9^5=295245" src="https://s0.wp.com/latex.php?latex=5+%5Ctimes+9%5E5%3D295245&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex">, which has six digits and a good endpoint for the loop. The loop itself cycles through the digits of each number and tests whether the sum of the fifth powers equals the number.</p>
<pre>
largest &lt;- 6 * 9^5
answer &lt;- 0
for (n in 2:largest) {
    power.sum &lt;-0
    i &lt;- n while (i > 0) {
        d &lt;- i %% 10
        i &lt;- floor(i / 10)
        power.sum &lt;- power.sum + d^5
    }
    if (power.sum == n) {
        print(n)
        answer &lt;- answer + n
    }
}
print(answer)
</pre>
<p>View the most recent version of this code on <a rel="nofollow" href="https://github.com/pprevos/r.prevos.net/blob/master/ProjectEuler/problem030.R" target="_blank">GitHub</a>.</p>
<p>The post <a rel="nofollow" href="https://r.prevos.net/digit-fifth-powers-euler-problem-30/" target="_blank">Digit fifth powers: Euler Problem 30</a> appeared first on <a rel="nofollow" href="https://r.prevos.net/" target="_blank">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="https://r.prevos.net/digit-fifth-powers-euler-problem-30/"> 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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/2c1ivW5m8XE" height="1"></a></div>