-
Notifications
You must be signed in to change notification settings - Fork 0
/
p9.cs
25 lines (24 loc) · 993 Bytes
/
p9.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
using System;
/*
* There exists exactly one Pythagorean triplet for which a + b + c = 1000, where a < b < c
* Find the product of a, b and c
*
*/
class p9
{
static void Main() {
for(int a = 1; a < 293; a++) //a is between 1 and 1000/(2+sqrt(2)) = 293. from geometry of right angled triangles
{
for(int b = 500-a; b < 500; b++) //b is between (500-a) and 500, since a+b>500 and b<500 from geometry
{
int c = (int) Math.Sqrt(a*a+b*b); //evaluate c and truncate any fractional part
if(a*a + b*b == c*c && a+b+c == 1000) //returns true if Math.Sqrt(a*a+b*b) was an integer and if a, b and c add to 1000
{
Console.WriteLine(a + " + " + b + " + " + c + " = " + (a+b+c)); //print result
Console.WriteLine("a * b * c = " + (a*b*c));
}
}
}
Console.WriteLine();
}
}