Rust Implementation of Problem 9

View source code here on GitHub!

Problem Solution

pub fn problems::p0009::p0009() -> utils::Answer
 1/*
 2Project Euler Problem 9
 3
 4
 5
 6Problem:
 7
 8A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
 9a**2 + b**2 = c**2
10
11For example, 3**2 + 4**2 = 9 + 16 = 25 = 5**2.
12
13There exists exactly one Pythagorean triplet for which a + b + c = 1000.
14Find the product abc.
15*/
16use crate::include::utils::Answer;
17
18pub fn p0009() -> Answer {
19    for c in 3.. {
20        let c_square = c * c;
21        for b in 2..c {
22            let b_square = b * b;
23            for a in 1..b {
24                let a_square = a * a;
25                if a_square + b_square == c_square && a + b + c == 1000 {
26                    return Answer::Int((a * b * c).into());
27                }
28            }
29        }
30    }
31    unreachable!();
32}

Tags: pythagorean-triple