forked from nayuki/Project-Euler-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp009.java
More file actions
36 lines (29 loc) · 932 Bytes
/
p009.java
File metadata and controls
36 lines (29 loc) · 932 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
/*
* Solution to Project Euler problem 9
* Copyright (c) Project Nayuki. All rights reserved.
*
* https://www.nayuki.io/page/project-euler-solutions
* https://github.com/nayuki/Project-Euler-solutions
*/
public final class p009 implements EulerSolution {
public static void main(String[] args) {
System.out.println(new p009().run());
}
/*
* Computers are fast, so we can implement a brute-force search to directly solve the problem.
* Note that a^2 + b^2 is bounded above by 2*(1000^2), which fits in a Java int type.
*/
private static final int PERIMETER = 1000;
public String run() {
for (int a = 1; a < PERIMETER; a++) {
for (int b = a + 1; b < PERIMETER; b++) {
int c = PERIMETER - a - b;
if (a * a + b * b == c * c) {
// It is now implied that b < c, because we have a > 0
return Integer.toString(a * b * c);
}
}
}
throw new AssertionError("Not found");
}
}