Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
GoikLectures
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Goik Martin
GoikLectures
Commits
5a7b5064
Commit
5a7b5064
authored
10 years ago
by
Goik Martin
Browse files
Options
Downloads
Patches
Plain Diff
Pythagorean triple exercise completed
parent
e6be2c24
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
Doc/Sd1/extraExercise.xml
+25
-0
25 additions, 0 deletions
Doc/Sd1/extraExercise.xml
Doc/Sd1/statements.xml
+216
-0
216 additions, 0 deletions
Doc/Sd1/statements.xml
with
241 additions
and
0 deletions
Doc/Sd1/extraExercise.xml
0 → 100644
+
25
−
0
View file @
5a7b5064
<?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>
This diff is collapsed.
Click to expand it.
Doc/Sd1/statements.xml
+
216
−
0
View file @
5a7b5064
...
...
@@ -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
<
1000; a++) {
for (int b = 1; b
<
1000; b++) {
for (int c = 1; c
<
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>
<
</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
<
= 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
<
= 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>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment