Skip to content

Commit 786fd5c

Browse files
add Problem09
1 parent 639bb5a commit 786fd5c

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.examplehub.projecteuler;
2+
3+
/**
4+
* A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
5+
* <p>
6+
* a^2 + b^2 = c^2
7+
* For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
8+
* <p>
9+
* There exists exactly one Pythagorean triplet for which a + b + c = 1000.
10+
* Find the product abc.
11+
* <p>
12+
* https://projecteuler.net/problem=9
13+
*/
14+
public class Problem09 {
15+
16+
public static int solution1(int n) {
17+
for (int a = 1; a <= n; a++) {
18+
for (int b = a + 1; b <= n; b++) {
19+
int c = n - a - b;
20+
if (b < c && (a * a + b * b == c * c)) {
21+
return a * b * c;
22+
}
23+
}
24+
}
25+
return -1;
26+
}
27+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.examplehub.projecteuler;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
class Problem09Test {
8+
9+
@Test
10+
void testSolution1() {
11+
assertEquals(60, Problem09.solution1(12));
12+
assertEquals(31875000, Problem09.solution1(1000));
13+
}
14+
}

0 commit comments

Comments
 (0)