<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-21-amicable-numbers/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/euler-problem-21-amicable-numbers/"> 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><a target="_blank" rel="nofollow" href="https://mathsgear.co.uk/products/amicable-numbers-pair-of-keyrings-nerd-romance"><img alt="Euler Problem 21: Amicable numbers key chain" src="https://i2.wp.com/cdn.shopify.com/s/files/1/0222/9748/products/hearts_grande.jpg?resize=300%2C212&ssl=1" data-recalc-dims="1"></a>Euler problem 21 takes us to the realm of <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Amicable_numbers">amicable numbers</a>, which are listed in sequence <a target="_blank" rel="nofollow" href="https://oeis.org/A259180">A259180</a> in the OEIS. Amicable, or friendly, numbers are the most romantic numbers known to maths. Amicable numbers serve absolutely no practical purpose, other than mathematical entertainment.</p>
<p>A related concept is a <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Perfect_number">perfect number</a>, which is a number that equals the sum of its proper divisors. Mathematicians have also defined <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Sociable_number">sociable numbers</a> and <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Betrothed_numbers">betrothed numbers </a>which are similar to amicable numbers. But perhaps these are for another Euler problem.</p>
<h2>Euler Problem 21 Definition</h2>
<p>Let <img title="d(n)" alt="d(n)" class="latex" src="http://s0.wp.com/latex.php?latex=d%28n%29&bg=ffffff&%23038;fg=000&%23038;s=0"> be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If <img title="d(a) = b" alt="d(a) = b" class="latex" src="http://s0.wp.com/latex.php?latex=d%28a%29+%3D+b&bg=ffffff&%23038;fg=000&%23038;s=0"> and <img title="d(b) = a" alt="d(b) = a" class="latex" src="http://s0.wp.com/latex.php?latex=d%28b%29+%3D+a&bg=ffffff&%23038;fg=000&%23038;s=0">, where <img title="a neq b" alt="a neq b" class="latex" src="http://s0.wp.com/latex.php?latex=a+%5Cneq+b&bg=ffffff&%23038;fg=000&%23038;s=0">, then <img title="a" alt="a" class="latex" src="http://s0.wp.com/latex.php?latex=a&bg=ffffff&%23038;fg=000&%23038;s=0"> and <img title="b" alt="b" class="latex" src="http://s0.wp.com/latex.php?latex=b&bg=ffffff&%23038;fg=000&%23038;s=0"> are an amicable pair and each of <img title="a" alt="a" class="latex" src="http://s0.wp.com/latex.php?latex=a&bg=ffffff&%23038;fg=000&%23038;s=0"> and <img title="b" alt="b" class="latex" src="http://s0.wp.com/latex.php?latex=b&bg=ffffff&%23038;fg=000&%23038;s=0"> are called amicable numbers.</p>
<p>For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore <img title="d(220) = 284" alt="d(220) = 284" class="latex" src="http://s0.wp.com/latex.php?latex=d%28220%29+%3D+284&bg=ffffff&%23038;fg=000&%23038;s=0">. The proper divisors of 284 are 1, 2, 4, 71 and 142; so, <img title="d(284) = 220" alt="d(284) = 220" class="latex" src="http://s0.wp.com/latex.php?latex=d%28284%29+%3D+220&bg=ffffff&%23038;fg=000&%23038;s=0">.</p>
<p><a target="_blank" rel="nofollow" href="https://projecteuler.net/problem=21">Evaluate the sum of all the amicable numbers under 10000</a>.</p>
<h1>Solution</h1>
<p>The first part of the code provides for a function to list all proper divisors for a given integer <em>x</em>. The loop determines the divisors for the numbers 220 to 10,000, calculates their sum and then checks if these numbers are amicable. When the code finds an amicable number, the counter jumps to the sum of the divisors to check for the next one.</p>
<pre>
proper.divisors &lt;- function(x) {
    divisors &lt;- vector()
    d &lt;- 1
    for (i in 1:floor(sqrt(x))) {
        if (x %% i == 0) {
            divisors[d] &lt;- i
            if (i != x/i) {
                d &lt;- d + 1
                divisors[d] &lt;- x / i
            }
            d &lt;- d + 1
        }
    }
    return(divisors)
}

answer &lt;- 0
n &lt;- 220
while (n &lt;= 10000) {
    div.sum &lt;- sum(proper.divisors(n)) - n
    if (n == sum(proper.divisors(div.sum)) - div.sum &amp; n != div.sum) {
        answer &lt;- answer + n + div.sum
        print(paste0(&quot;(&quot;, n, &quot;,&quot;, div.sum, &quot;)&quot;))
        n &lt;- div.sum
    }
    n &lt;- n + 1
}
print(answer)
</pre>
<p>Amicable numbers were known to the <a title="Pythagoreanism" target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Pythagoreanism">Pythagoreans</a>, who credited them with many mystical properties. Before we had access to computers, finding amicable numbers was a task that required a lot of patience. No algorithm can systematically generate all amicable numbers, and until 1946 only 390 pairs were known. Medieval Muslim mathematicians developed several formulas to create amicable numbers, but the only way to be complete is using brute force.</p>
<p><iframe width="450" allowfullscreen="true" src="https://www.youtube.com/embed/fUSZBVYZdKY?version=3&rel=1&%23038;fs=1&%23038;autohide=2&%23038;showsearch=0&%23038;showinfo=1&%23038;iv_load_policy=1&%23038;wmode=transparent" type="text/html"></iframe></p>
<p>The post <a target="_blank" rel="nofollow" href="http://r.prevos.net/euler-problem-21-amicable-numbers/">Euler Problem 21: Amicable Numbers</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-21-amicable-numbers/"> 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/LQUVFX6Vzu8"></a></div>