Solution to Project Euler Problem 9: Special Pythagorean triplet - A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2 For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.
Updated: Oct. 3, 2023 — Training Time: 2 minutes
Overseen by: Archangel Macsika
All Training Resources
Scroll for more menu list
Topic: Project Euler Problem 9: Special Pythagorean triplet.
Difficulty: Easy.
Objective: A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
Input: None.
Expected Output: 31875000.
Sikademy Solution in Java Programming Language
package sikademy;
/**
*
* @author Archangel Macsika
* Copyright (c) Sikademy. All rights reserved
*/
public class SikademyEulerSolution {
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");
}
public static void main(String[] args) {
SikademyEulerSolution solution = new SikademyEulerSolution();
System.out.println(solution.run());
}
}
Sikademy Solution in Python Programming Language
#
# @author Archangel Macsika
# Copyright (c) Sikademy. All rights reserved.
#
def compute():
PERIMETER = 1000
for a in range(1, PERIMETER + 1):
for b in range(a + 1, PERIMETER + 1):
c = PERIMETER - a - b
if a * a + b * b == c * c:
# It is now implied that b < c, because we have a > 0
return str(a * b * c)
if __name__ == "__main__":
print(compute())