// Pythagoras-03.cpp

/* Listing of right angle triangles with whole number sides e.g. 3 4 5

Need only try possible values below Squareroot of side_1 sqaured + side_2 squared

I want to treat triangle (3 4 5) as a duplicate of (4 3 5)

Achieve this by only selecting results with side_1 > side_2

Also need to remove multiples such as (6 8 10) (12 16 20)

all multiples of (3 4 5).

Do this by seeing if greatest common denominator > 1 by calling

gcd with side-1 and side_2 to find their gcd and then call gcd

again with this gcd and hyp.

If this is 1 then combination is not a multiple

gcd uses Euclidean method to find gcd.

Virtual printer provides an alternative output source for printer

redirecting to D:\\My Documents\\Temp\\VPRINTER.OUT.

I use this as the console does not keep all of output in lon runs.

You can comment it out if you wish.

*/

#include

#include

#include

#include "D:\My Documents\Borland Studio Projects\mytools.h"

// #include "C:\Documents and Settings\Rod Talboys\My Documents\Borland Studio Projects\mytools.h"

void get_range (int& min, int& max); //out

int gcd (int a, int b); // in-out

void main()

{

// Virtual printer

// This file provides an alternative output source for printer

ofstream vprn ("D:\\My Documents\\Temp\\VPRINTER.OUT");

int max, min, side_1, side_2, hyp, count;

int hypmax, hypmin;

get_range (min, max);

cout << "Pythagorean triangles between " << min << " and " << max << endl;

vprn << "Pythagorean triangles between " << min << " and " << max << endl;

count=0;

for (side_1=min;side_1<=max;side_1++)

{

for (side_2=min;side_2<=max;side_2++)

{

// check if side_1*side_1 + side_2*side_2 is perfect square

// establish hypmin and hypmax

if (side_1>side_2) (hypmin=side_1);

else (hypmin=side_2); // removes surplus

hypmax= floor(sqrt(side_1*side_1 + side_2*side_2)); // processing

for (hyp=hypmin;hyp<=hypmax;hyp++)

{

if ((hyp*hyp==(side_1*side_1 + side_2*side_2))&& (side_1 < side_2))

{

if ((gcd ( hyp, gcd (side_1, side_2))) == 1)

{

cout << side_1 << " " << side_2 << " " << hyp << endl;

vprn << side_1 << " " << side_2 << " " << hyp << endl;

count++ ;

}

}

}

}

}

cout << "There are " << count <<" Pythagorean triangles between " << min << " and " << max << endl;

vprn << "There are " << count <<" Pythagorean triangles between " << min << " and " << max << endl;

system("PAUSE");

return 0;

}

void get_range (int& min, int& max) //out

{

bool valid; // valid for input and prime for prime number

valid = false; // initialise values

max = 0;

while (!valid)

{

cout << "Enter min and max (less than 1,000) values for smaller sides: " << endl;

cin >> min >> max;

if (max > min && max <= 1000)

valid = true;

else

cout << " Invalid entry try again." << endl;

}

}

int gcd (int a, int b)

// Use the Euclidean algorithm to calculate and

// return the greatest common divisor of a and b.

{

int r;

a = labs(a); b=labs(b);

while (b>0)

{ r = a%b; a = b; b = r; }

return a;

}

## Monday, June 11, 2007

### C++ Program - Identify Pythagoras Triangle

Subscribe to:
Post Comments (Atom)

## About Me

- Rod T
- Husband, dad and grandad. Physics graduate from London University in late 60s. Retired from IBM company in 2000 after 27 years as both Sytems Engineer and Salesman. Interests include photography, nature, science, maths, walking, travel. I like facts as a basis of opinion and not opinion that is assumed to be fact.

## No comments:

Post a Comment