forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp075.java
More file actions
87 lines (67 loc) · 1.84 KB
/
p075.java
File metadata and controls
87 lines (67 loc) · 1.84 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Solution to Project Euler problem 75
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
import java.util.HashSet;
import java.util.Set;
public final class p075 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p075().run());
}
private static final int LIMIT = 1500000;
public String run() {
/*
* Pythagorean triples theorem:
* Every primitive Pythagorean triple with a odd and b even can be expressed as
* a = st, b = (s^2-t^2)/2, c = (s^2+t^2)/2, where s > t > 0 are coprime odd integers.
*/
Set<IntTriple> triples = new HashSet<>();
for (int s = 3; s * s <= LIMIT; s += 2) {
for (int t = s - 2; t > 0; t -= 2) {
if (Library.gcd(s, t) == 1) {
int a = s * t;
int b = (s * s - t * t) / 2;
int c = (s * s + t * t) / 2;
if (a + b + c <= LIMIT)
triples.add(new IntTriple(a, b, c));
}
}
}
byte[] ways = new byte[LIMIT + 1];
for (IntTriple triple : triples) {
int sum = triple.a + triple.b + triple.c;
for (int i = sum; i < ways.length; i += sum)
ways[i] = (byte)Math.min(ways[i] + 1, 2); // Increment but saturate at 2
}
int count = 0;
for (int x : ways) {
if (x == 1)
count++;
}
return Integer.toString(count);
}
private static final class IntTriple {
public final int a;
public final int b;
public final int c;
public IntTriple(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public boolean equals(Object obj) {
if (!(obj instanceof IntTriple))
return false;
else {
IntTriple other = (IntTriple)obj;
return a == other.a && b == other.b && c == other.c;
}
}
public int hashCode() {
return a + b + c;
}
}
}