I'm currently working on ProjectEuler.net's problem no. 39 and it states "If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p <= 1000, is the number of solutions maximised?"

Now, the program I have written now:

#include <iostream>
#include <cmath>
#include <fstream>
#include <string.h>
using namespace std;

void PYTHAG(int PERIMETER)
{
	long double A;
	long double B;
	long double C;

	for(A = 1; A < PERIMETER; A++)
	{
		for(B = 1; B < C; B++)
		{
			C = (sqrt((pow(A,2))+(pow(B,2))));

			if((A + B + C) == PERIMETER)
			{
				cout << "{ " << A << ", " << B << ", " << C << " }" << endl;
			}
		}
	}
}

int main(int PERIMETER)
{
	cout << "Enter your desired perimeter." << endl << endl;
	cin >> PERIMETER;

	PYTHAG(PERIMETER);

	cin.get();
	cin.get();
	return 0;
}

Allows me to get the solutions set for the sample problem (perimeter = 120). However , it repeats the same numbers over and over again, such as: {20,48,52} and {48,20,52} and so on. I was wondering if it's possible to take the three numbers the program comes up with and save it to a file then have it read the file, when it goes through the loop again, and determine whether or not those numbers have been printed already. If they have, then it should go onto the next set, if not, it should print them.

- xtremerocker

Recommended Answers

All 2 Replies

> for(B = 1; B < C; B++)
Perhaps your code is broken, because C is uninitialised the first time around.
Perhaps < A ?

That worked, thanks for the reply.

- xtremerocker

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.