Pythagorean Triples Solution



A set of integers, (a; b; c), satisfying the following equation is known as a set of Pythagorean Triple.

a2 + b2 = c2:


In this program assignment, please write a C program to find all Pythagorean Triples such that a b c 20000.

Example program output is shown below.

  • ./a.out

Pythagorean Triple #1 is (3,4,5)

Pythagorean Triple #2 is (6,8,10)

Total number of Pythagorean triples found is xxxxx

Try to write your program as efficient as possible. To measure the execution time of a program, Unix system provides a time command. For example,

  • time ./a.out

can produce an output line at the end of the program execution as following.

3.943u 0.002s 0:03.96 99.4% 0+0k 0+0io 0pf+0w

where the first number is the CPU time used by the the a.out program measured in seconds. If needed, you can also use the built-in math function that calculates the square root of a

double precision number.

double sqrt(double x);

As shown above, it takes a double precision number as the input parameter and returns a double precision number which is the square root of x. In order to use this function, you’ll need to include the math header as

#include <math.h>

And, the compilation command is a little different as

  • gcc lab03.c -lm

The added option -lm links the program with the math library such that sqrt function can be found.



  1. Create a directory lab03 and use it as the working directory.

  1. Name your program source file as lab03.c.

  1. The first few lines of your program should be comments as the following.

/* EE231002 Lab03 Pythagorean Triples ID, Name



  1. After finishing editing your source file, you can execute the following command to compile it,

$ gcc lab03.c -lm

If no compilation errors, the executable file, a.out, should be generated, and you can execute it by typing

$ time ./a.out

  1. Typical outputs of the program execution have been shown above. You should try to minimize the execution time.

  1. After you finish verifying your program, you can submit your source code by

$ ee231002/bin/submit lab03 lab03.c

If you see a ”submitted successfully” message, then you are done. In case you want to check which file and at what time you submitted your labs, you can type in the following command:

$ ee231002/bin/subrec lab03

It will show the submission records for lab03.