-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrimitivePythagoreanTriples.java
More file actions
60 lines (57 loc) · 1.53 KB
/
PrimitivePythagoreanTriples.java
File metadata and controls
60 lines (57 loc) · 1.53 KB
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
import java.lang.Math;
import java.util.*;
class PrimitivePythagoreanTriples
{
public static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static int input(String prompt)
{
System.out.print(prompt);
return new Scanner(System.in).nextInt();
}
public static void main(String args[])
{
System.out.println("Primitive Pythagorean Triples between n1 and n2.");
int n1 = input("Enter n1 :\t");
int n2 = input("Enter n2 :\t");
if (n1 > n2)
n1 = n1 + n2 - (n2 = n1);
if (n1 < 3)
n1 = 3;
if (n1 != n2 && n2 > 4)
{
int b=1, i=0, f=1, n=(int)Math.log10(n2) + 1;
for (int a = 2; ; a += 2)
{
if (gcd(a, b) != 1)
continue;
int p = 2*a*b, q = a*a - b*b;
if (p > q)
p = p + q - (q = p);
if (p < n1)
{
f = 1;
continue;
}
int r = a*a + b*b;
if (r <= n2 && p >= n1)
{
i++;
f = 1;
System.out.printf("%"+n+"d (%"+n+"d, %"+n+"d, %"+n+"d)\n", i, p, q, r);
}
else if (f != 0)
{
a = b++;
f = 0;
}
else
break;
}
}
}
}