Project Euler in R Problem 9

[This article was first published on Frank Portman, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In Problem 9 of Project Euler we are tasked with finding the product (abc) of the Pythagorean Triplet (a, b, c) such that a + b + c = 1000.

A Pythagorean triplet is a set of three natural numbers such that a2 + b2 = c2.

To solve this problem, we first see that c = (a2 + b2)1/2. Without loss of generality, we can only run the for loop for a and b, since c will be uniquely determined given a certain a and b.

The code I used:

1
2
3
4
5
6
7
8
9
10
11
<span class="line"><span class="kr">for</span> <span class="p">(</span>a <span class="kr">in</span> <span class="m">1</span><span class="o">:</span><span class="m">499</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line"> <span class="kr">for</span> <span class="p">(</span>b <span class="kr">in</span> <span class="m">1</span><span class="o">:</span><span class="m">499</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">   <span class="kr">if</span> <span class="p">(</span>a <span class="o">+</span> b <span class="o">+</span> <span class="kp">sqrt</span><span class="p">(</span>a<span class="o">^</span><span class="m">2</span> <span class="o">+</span> b<span class="o">^</span><span class="m">2</span><span class="p">)</span> <span class="o">==</span> <span class="m">1000</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">     <span class="kp">print</span><span class="p">(</span>a <span class="o">*</span> b <span class="o">*</span> <span class="kp">sqrt</span><span class="p">(</span>a<span class="o">^</span><span class="m">2</span> <span class="o">+</span> b<span class="o">^</span><span class="m">2</span><span class="p">))</span>
</span><span class="line">     <span class="kr">break</span>
</span><span class="line">   <span class="p">}</span>
</span><span class="line"> <span class="p">}</span>
</span><span class="line"> <span class="kr">if</span> <span class="p">(</span>a <span class="o">+</span> b <span class="o">+</span> <span class="kp">sqrt</span><span class="p">(</span>a<span class="o">^</span><span class="m">2</span> <span class="o">+</span> b<span class="o">^</span><span class="m">2</span><span class="p">)</span> <span class="o">==</span> <span class="m">1000</span><span class="p">)</span> <span class="p">{</span>
</span><span class="line">   <span class="kr">break</span>
</span><span class="line"> <span class="p">}</span>
</span><span class="line"><span class="p">}</span>
</span>

I use nested for loops to test values of a and b between 1 and 499. a and b cannot take values greater than 499 because then a + b + sqrt(a2 + b2) would be greater than 1000.

The if statement in the nested loops checks to see whether a, b, and c add up to 1000. If they do, it prints their product and then breaks the loop. This is a short script which produces the correct answer in a few milliseconds. The trick here was expressing c in terms of a and b to reduce the amount of loops we need to run.

To leave a comment for the author, please follow the link and comment on their blog: Frank Portman.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)