Due Saturday, February 26, 11:59 P.M.
- Overview
- Assignment
- Hints
The purpose of this lab is to introduce you to using named loops,
DO-WHILE loops, counting loops, and the CYCLE and
EXIT statements in fortran.
Top
Write a fortran program called pyth3 that finds all pythagorean triples
x2+y2=z2 with the values of
x,y, and z lying in a specified range. Your program
must satisfy the following requirements:
- You should error check the input provided by the user. You should
continue prompting the user and reading in new input until valid input
is provided.
- You should not print any triple more than once. I.e. for the range
of 1-5, your program should output
3 4 5
rather than
3 4 5
4 3 5
- You should only check for primitive pythagorean triples, i.e.
those tripes x2+y2=z2 such
that gcd(x,y)=1. So for the range 1-10, your output should be
3 4 5
rather than
3 4 5
6 8 10
- You should optimize your code as much as possible. At a minimum,
you should avoid testing values that cannot belong to triples falling in
the requested range. For example, if the range of 1-10 is specified,
when checking the possible x,y pairs with x=8, you should
not check any y-value larger than 6, since there is no solution with
x=8,y>6, and 1<=z<=10.
Top
- Begin by prompting the user for the specified range, and write
a nested counting loop that iterates through/writes out all possible
x,y values in this range.
- Now check whether each x,y pair is part of a pythagorean triple,
and modify your code to only write out the appropriate x,y pairs.
- Next, edit the boundary values on one of your loops to avoid checking
redundant solutions.
- Add conditional CYCLE statements to implement any optimizations.
You will probably need to use named loops here.
- Now go back and add a test to skip over any values that are not primitive.
- Finally, test your program against the various lists of
primitive pythagorean triples available on the web, such as this one.
Top
Back to the course information page