-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9_special_pythagorean_triplet.cpp
More file actions
55 lines (50 loc) · 1016 Bytes
/
9_special_pythagorean_triplet.cpp
File metadata and controls
55 lines (50 loc) · 1016 Bytes
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <iomanip>
using namespace std;
const int sum=1000;
const int limit=500;
const int digits=4;
int main()
{
original();
optimized();
}
void original()
{
int i,j,k,count=0;
for (i=1; i<=limit; i++)
for (j=i; j<=limit; j++)
{
if (i*i+j*j==(sum-i-j)*(sum-i-j));
{
count++;
cout<<left;
cout<<setw(digits)<<count
<<setw(digits)<<i
<<setw(digits)<<j
<<setw(digits)<<k
<<setw(digits)<<i*j*k<<endl;
}
}
return 0;
}
void optimized()
{
int i,j,count=0;
for (i=3; i<=(sum-3)/3; i++) //i<j<(sum-i-j). The least value of sum is when the numbers are consecutive: a,a+1,a+2; Hence sum>=3a+3
for (j=i+1; j<(sum-i)/2; j++) //i<j<(sum-i-j) => j<sum-i-j => 2j<sum-i => j<(sum-i)/2
{
if (i*i+j*j==(sum-i-j)*(sum-i-j))
{
count++;
cout<<right;
cout<<count
<<setw(digits)<<i
<<setw(digits)<<j<<endl;
}
}
return 0;
}
void even_better()
{
}