forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pythagorean_Triples.cpp
62 lines (53 loc) · 1.11 KB
/
Pythagorean_Triples.cpp
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
56
57
58
59
60
61
62
//C++ Program to generate Pythagorean Triples within a user-given limit
#include <bits/stdc++.h>
using namespace std;
//Function for computing Pythagorean Triples
void GetTriples(int& l)
{
//Starting values of m=2, n=1 are mapped to a=3, b=4, c=5, which is the smallest Pythagorean triple
int a = 0, b = 0, c = 0, m = 2, n = 1;
while (c < l)
{
//For every m, evaluate a,b,c for all values of n from 1 to m-1
n = 1;
while (n < m)
{
//Evaluating a, b, c using relation between a,b,c and m,n
a = (int)(pow(m, 2)) - (int)(pow(n, 2));
b = 2 *m * n;
c = (int)(pow(m, 2)) + (int)(pow(n, 2));
//Checking for limit being exceeded
if (c > l)
break;
//Display computed Triple
cout << a << " " << b << " " << c << "\n";
n++;
}
m++;
}
}
//Driver function
int main()
{
int l;
//Prompts user for input
cout << "Enter limit (>4) :";
cin >> l;
GetTriples(l);
return 0;
}
/*
Sample I/O:
Enter limit: 30
3 4 5
8 6 10
5 12 13
15 8 17
12 16 20
7 24 25
24 10 26
21 20 29
Time Complexity - O(k)[k = number of triples generated]
Space Complexity - O(1)
References: https://youtu.be/n6vL2KiWrD4
*/