<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/euler-problem-29-distinct-powers/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/euler-problem-29-distinct-powers/"> 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 29 is another <a rel="nofollow" href="http://r.prevos.net/tag/permutation/" target="_blank">permutation</a> problem that is quite easy to solve using brute force. The <a rel="nofollow" href="http://www.mathblog.dk/project-euler-29-distinct-terms-sequence-ab/" target="_blank">MathBlog</a> site by Kristian Edlund has a nice solution using only pen and paper.</p>
<p>Raising number to a power can have interesting results. The video below explains why this pandigital formula approximates <img alt="e" title="e" src="http://s0.wp.com/latex.php?latex=e&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> to billions of decimals:</p>
<p><img alt="(1 + 9^{-4^{6 times 7}})^{3^{2^{85}}} approx e " title="(1 + 9^{-4^{6 times 7}})^{3^{2^{85}}} approx e " src="http://s0.wp.com/latex.php?latex=%281+%2B+9%5E%7B-4%5E%7B6+%5Ctimes+7%7D%7D%29%5E%7B3%5E%7B2%5E%7B85%7D%7D%7D+%5Capprox+e+&bg=ffffff&%23038;fg=000&%23038;s=4" class="latex"></p>
<p><iframe allowfullscreen="true" src="https://www.youtube.com/embed/xgBGibfLD-U?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 29 Definition</h2>
<p>Consider all integer combinations of: <img alt="a^b " title="a^b " src="http://s0.wp.com/latex.php?latex=a%5Eb+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> for <img alt="2 leq a leq 5 " title="2 leq a leq 5 " src="http://s0.wp.com/latex.php?latex=2+%5Cleq+a+%5Cleq+5+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> and <img alt="leq b leq 5 " title="leq b leq 5 " src="http://s0.wp.com/latex.php?latex=%5Cleq+b+%5Cleq+5+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex">.</p>
<p><img alt="2^2=4, quad 2^3 = 8,quad 2^4 = 16,quad 2^5 = 32 " title="2^2=4, quad 2^3 = 8,quad 2^4 = 16,quad 2^5 = 32 " src="http://s0.wp.com/latex.php?latex=2%5E2%3D4%2C%C2%A0%5Cquad+2%5E3+%3D+8%2C%5Cquad+2%5E4+%3D+16%2C%5Cquad+2%5E5+%3D+32+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"></p>
<p><img alt="3^2 = 9,quad 3^3 = 27,quad 3^4 = 81,quad 3^5 = 243 " title="3^2 = 9,quad 3^3 = 27,quad 3^4 = 81,quad 3^5 = 243 " src="http://s0.wp.com/latex.php?latex=3%5E2+%3D+9%2C%5Cquad+3%5E3+%3D+27%2C%5Cquad+3%5E4+%3D+81%2C%5Cquad+3%5E5+%3D+243+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"></p>
<p><img alt="4^2 = 16,quad 4^3 = 64,quad 4^4 = 256, quad 4^5 = 1024 " title="4^2 = 16,quad 4^3 = 64,quad 4^4 = 256, quad 4^5 = 1024 " src="http://s0.wp.com/latex.php?latex=4%5E2+%3D+16%2C%5Cquad+4%5E3+%3D+64%2C%5Cquad+4%5E4+%3D+256%2C+%5Cquad+4%5E5+%3D+1024+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"></p>
<p><img alt="5^2 = 25,quad 5^3 = 125,quad 5^4 = 625,quad 5^5 = 3125 " title="5^2 = 25,quad 5^3 = 125,quad 5^4 = 625,quad 5^5 = 3125 " src="http://s0.wp.com/latex.php?latex=5%5E2+%3D+25%2C%5Cquad+5%5E3+%3D+125%2C%5Cquad+5%5E4+%3D+625%2C%5Cquad+5%5E5+%3D+3125+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"></p>
<p>If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms:</p>
<p><img alt="4,  8,  9,  16,  25,  27,  32,  64,  81,  125,  243,  256, 625,  1024,  3125 " title="4,  8,  9,  16,  25,  27,  32,  64,  81,  125,  243,  256, 625,  1024,  3125 " src="http://s0.wp.com/latex.php?latex=4%2C+%5C+8%2C+%5C+9%2C+%5C+16%2C+%5C+25%2C+%5C+27%2C+%5C+32%2C+%5C+64%2C+%5C+81%2C+%5C+125%2C+%5C+243%2C+%5C+256%2C%5C+625%2C+%5C+1024%2C+%5C+3125+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"></p>
<p>How many <a rel="nofollow" href="https://projecteuler.net/problem=29" target="_blank">distinct terms are in the sequence</a> generated by <img alt="a^b " title="a^b " src="http://s0.wp.com/latex.php?latex=a%5Eb+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> for <img alt="2 leq a leq 100 " title="2 leq a leq 100 " src="http://s0.wp.com/latex.php?latex=2+%5Cleq+a+%5Cleq+100+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> and <img alt="2 leq b leq 100 " title="2 leq b leq 100 " src="http://s0.wp.com/latex.php?latex=2+%5Cleq+b+%5Cleq+100+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex">?</p>
<h2>Brute Force Solution</h2>
<p>This code simply calculates all powers from <img alt="2^2 " title="2^2 " src="http://s0.wp.com/latex.php?latex=2%5E2+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> to <img alt="2^{1000} " title="2^{1000} " src="http://s0.wp.com/latex.php?latex=2%5E%7B1000%7D+&bg=ffffff&%23038;fg=000&%23038;s=0" class="latex"> and determines the number of unique values. Since we are only interested in their uniqueness and not the precise value, there is no need to use <a rel="nofollow" href="http://r.prevos.net/tag/large-integers/" target="_blank">Multiple Precision Arithmetic</a>.</p>
<pre>
# Initialisation
target &lt;- 100
terms &lt;- vector()
i &lt;- 1
# Loop through values of a and b and store powers in vector
for (a in 2:target) {
   for (b in 2:target) {
     terms[i] &lt;- a^b
     i &lt;- i + 1
   }
}
# Determine the number of distinct powers
answer &lt;- length(unique(terms))
print(answer)
</pre>
<p>The post <a rel="nofollow" href="http://r.prevos.net/euler-problem-29-distinct-powers/" target="_blank">Euler Problem 29: Distinct Powers</a> appeared first on <a rel="nofollow" href="http://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="http://r.prevos.net/euler-problem-29-distinct-powers/"> 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/kY0IN_95IN4" height="1"></a></div>