Skip to content
Snippets Groups Projects
Commit 5a7b5064 authored by Goik Martin's avatar Goik Martin
Browse files

Pythagorean triple exercise completed

parent e6be2c24
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<chapter version="5.0" xml:id="sd1ExtraExercise" xmlns="http://docbook.org/ns/docbook"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:m="http://www.w3.org/1998/Math/MathML"
xmlns:html="http://www.w3.org/1999/xhtml"
xmlns:db="http://docbook.org/ns/docbook">
<title>Loops</title>
<section xml:id="sd1ExternalExercises">
<title>Recommended exercises</title>
<para></para>
</section>
<section xml:id="sd1NewLectureExercises">
<title>Exercises recently added to these lecture notes</title>
<para></para>
</section>
</chapter>
......@@ -97,4 +97,220 @@
</qandadiv>
</qandaset>
</section>
<section xml:id="sw1SectionPythagoreanTriples">
<title>Pythagorean triples</title>
<qandaset defaultlabel="qanda" xml:id="sw1QandaPythagoreanTriples">
<title>Finding pythagorean triples</title>
<qandadiv>
<qandaentry>
<question>
<para>Read the <link
xlink:href="http://en.wikipedia.org/wiki/Pythagorean_triple">definition
of pythagorean triples</link>.</para>
<para>Find all <link
xlink:href="http://en.wikipedia.org/wiki/Pythagorean_triple">pythagorean
triples</link> (a,b,c) for which which in addition to their
definition the restriction <inlineequation>
<m:math display="inline">
<m:mrow>
<m:mrow>
<m:mi>a</m:mi>
<m:mo>+</m:mo>
<m:mi>b</m:mi>
<m:mo>+</m:mo>
<m:mi>c</m:mi>
</m:mrow>
<m:mo>=</m:mo>
<m:mi>1000</m:mi>
</m:mrow>
</m:math>
</inlineequation> holds</para>
<tip>
<para>Think of writing a program which creates all triplets
(a,b,c) not yet obeying the restriction a + b + c = 1000:</para>
<informaltable border="1">
<tr>
<th>a</th>
<th>b</th>
<th>c</th>
</tr>
<tr>
<td>1</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>2</td>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>1000</td>
<td>1000</td>
<td>1000</td>
</tr>
</informaltable>
<para>This might be accomplished by implementing three nested
loops:</para>
<programlisting language="java"> for (int a = 1; a &lt; 1000; a++) {
for (int b = 1; b &lt; 1000; b++) {
for (int c = 1; c &lt; 1000; c++) {
System.out.println("(" + a + ", " + b + ", " + c + ")");
}
}
}</programlisting>
<para>The above code will run for ages! Now try to alter this
code to generate only triplets (a,b,c) simultaneously obeying:
</para>
<orderedlist>
<listitem xml:id="sw1PhytagoreanTripletCondition1">
<para><inlineequation>
<m:math display="inline">
<m:mrow>
<m:mi>a</m:mi>
<m:mo></m:mo>
<m:mi>b</m:mi>
<m:mo>&lt;</m:mo>
<m:mi>c</m:mi>
</m:mrow>
</m:math>
</inlineequation></para>
</listitem>
<listitem xml:id="sw1PhytagoreanTripletCondition2">
<para><inlineequation>
<m:math display="inline">
<m:mrow>
<m:mrow>
<m:mi>a</m:mi>
<m:mo>+</m:mo>
<m:mi>b</m:mi>
<m:mo>+</m:mo>
<m:mi>c</m:mi>
</m:mrow>
<m:mo>=</m:mo>
<m:mi>1000</m:mi>
</m:mrow>
</m:math>
</inlineequation></para>
</listitem>
</orderedlist>
<para>When you have done so filter your output for the set of
all triplets which obey the third restriction<inlineequation>
<m:math display="inline">
<m:mrow>
<m:mrow>
<m:msup>
<m:mi> a</m:mi>
<m:mn>2</m:mn>
</m:msup>
<m:mo>+</m:mo>
<m:msup>
<m:mi>b</m:mi>
<m:mn>2</m:mn>
</m:msup>
</m:mrow>
<m:mo>=</m:mo>
<m:msup>
<m:mi>c</m:mi>
<m:mn>2</m:mn>
</m:msup>
</m:mrow>
</m:math>
</inlineequation> as well.</para>
</tip>
</question>
<answer>
<para>The last triplet obeying both restrictions <xref
linkend="sw1PhytagoreanTripletCondition1"/> and <xref
linkend="sw1PhytagoreanTripletCondition2"/> is (333,333,324). Due
to restriction <xref linkend="sw1PhytagoreanTripletCondition1"/>
we need just two rather than three nested loops to generate all
required triplets:</para>
<programlisting language="java"> public static void main(String[] args) {
for (int a = 1; a &lt;= 333; a++) {
final int b_plus_c = 1000 - a, // the sum of b and c
aSquare = a * a; // Save value to be calculated only once (not inside subsequent loop)
final int bMaximum;
if (0 == b_plus_c % 2) { // Sum of b and c is even?
bMaximum = b_plus_c / 2 - 1; // c must be allowed to become larger then b
} else {
bMaximum = b_plus_c / 2; // c's minimum value will become bMaximum + 1
}
for (int b = a; b &lt;= bMaximum; b++) {
final int c = b_plus_c - b;
if (aSquare + b * b == c * c) { // Filter desired triplet values
System.out.println("(" + a + ", " + b + ", " + c + ")");
}
}
}
}</programlisting>
<para>This yields exactly one triplet:</para>
<programlisting language="none">(200, 375, 425)</programlisting>
</answer>
</qandaentry>
</qandadiv>
</qandaset>
</section>
</chapter>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment