<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-9-special-pythagorean-triple/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://r.prevos.net/euler-problem-9/"> 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>
<h2>Euler Problem 9 Definition</h2>
<div class="wp-caption"><img sizes="(max-width: 300px) 100vw, 300px" data-image-description="&lt;p&gt;Euler Problem 9: Pythagorean Triples&lt;/p&gt;
" data-orig-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/02/Pythagorean_Triples_Scatter_Plot.png?fit=600%2C584" data-orig-size="600,584" data-image-title="Pythagorean_Triples_Scatter_Plot" class="wp-image-120" data-comments-opened="1" data-recalc-dims="1" data-attachment-id="120" data-permalink="http://r.prevos.net/euler-problem-9/pythagorean_triples_scatter_plot/" alt="Euler Problem 9: Pythagorean Triples" data-medium-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/02/Pythagorean_Triples_Scatter_Plot.png?fit=300%2C292" data-large-file="https://i2.wp.com/r.prevos.net/wp-content/uploads/sites/11/2017/02/Pythagorean_Triples_Scatter_Plot.png?fit=584%2C568" 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/02/Pythagorean_Triples_Scatter_Plot.png?resize=300%2C292">
<p class="wp-caption-text">Scatter plot of the legs (a,b) of the first Pythagorean triples with <em>a</em> and <em>b</em> less than 6000. Negative values are included to illustrate the parabolic patterns. By <a title="User:Dearjean13 (page does not exist)" target="_blank" rel="nofollow" href="http://commons.wikimedia.org/w/index.php?title=User:Dearjean13&action=edit&redlink=1">Dearjean13</a> – <span lang="en">Own work</span>, <a title="Creative Commons Attribution-Share Alike 4.0" target="_blank" rel="nofollow" href="http://creativecommons.org/licenses/by-sa/4.0">CC BY-SA 4.0</a>, <a target="_blank" rel="nofollow" href="https://commons.wikimedia.org/w/index.php?curid=48979795">Link</a></p>
</div>
<p>A <a target="_blank" rel="nofollow" href="https://en.wikipedia.org/wiki/Pythagorean_triple">Pythagorean triple</a> is a set of three natural numbers, <img title="a &lt; b &lt; c " alt="a &lt; b &lt; c " class="latex" src="http://s0.wp.com/latex.php?latex=a+%3C+b+%3C+c+&bg=ffffff&%23038;fg=000&%23038;s=0">, for which, <img title="a^2 + b^2 = c^2 " alt="a^2 + b^2 = c^2 " class="latex" src="http://s0.wp.com/latex.php?latex=a%5E2+%2B+b%5E2+%3D+c%5E2+&bg=ffffff&%23038;fg=000&%23038;s=0">. For example:</p>
<p><img title="3^2 + 4^2 = 9 + 16 = 25 = 5^2 " alt="3^2 + 4^2 = 9 + 16 = 25 = 5^2 " class="latex" src="http://s0.wp.com/latex.php?latex=3%5E2+%2B+4%5E2+%3D+9+%2B+16+%3D+25+%3D+5%5E2+&bg=ffffff&%23038;fg=000&%23038;s=0">.</p>
<p>There exists exactly one Pythagorean triplet for which <img title="a + b + c = 1000 " alt="a + b + c = 1000 " class="latex" src="http://s0.wp.com/latex.php?latex=a+%2B+b+%2B+c+%3D+1000+&bg=ffffff&%23038;fg=000&%23038;s=0">.</p>
<p>Find the product of <em>a</em>, <em>b</em> and <em>c</em>.</p>
<h2>Brute Force Solution</h2>
<p>This solution uses brute force and checks all combinations of <em>a</em>, <em>b</em> and <em>c</em>. To limit the solution space I used the fact that <em>a</em> &lt; <em>b</em> &lt; <em>c</em>, which implies that <em>a</em> &lt; <em>s</em>/3,  and <em>a</em> &lt; <em>b</em> &lt; <em>s</em>/2, where <em>s</em> is the sum of the three sides.</p>
<pre>
a &lt;- 0
b &lt;- 0
c &lt;- 0
s &lt;- 1000
found &lt;- FALSE
for (a in 1:floor((s/3))) {
    for (b in a:(s/2)) {
        c &lt;- s - a - b
        if (a^2 + b^2 == c^2) {
            found &lt;- TRUE
            break
        }
    }
    if (found) 
        break
}
answer &lt;- a * b * c
</pre>
<h2></h2>
<p> </p>
<p>The post <a target="_blank" rel="nofollow" href="http://r.prevos.net/euler-problem-9/">Euler Problem 9 : Special Pythagorean Triple</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-9/"> 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/Iqr5GSUZYPw"></a></div>