-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9.rs
41 lines (32 loc) · 1.01 KB
/
9.rs
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
/*
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 .
*/
//! Notes:
// a + b + c = n
// c = n - a - b
// a^2 + b^2 = c^2
// a^2 + b^2 = (n - a - b)^2
// 0 = 2ab - 2an - 2bn + n^2
// 2an - n^2 = b(2a - 2n)
// b = (2an - n^2)/(2a - 2n)
// For a given value a, with a sum of n
// b = (2an - n^2)/(2a - 2n)
// c = n - a - b
fn triplet_with_sum(sum: isize) -> Option<(isize, isize, isize)> {
for a in 1..(sum + 1) {
let b: isize = ((2 * a * sum) - sum.pow(2)) / (2 * (a - sum));
let c: isize = sum - a - b;
if a.pow(2) + b.pow(2) == c.pow(2) {
return Some((a, b, c));
}
}
return None;
}
pub fn main() {
let (a, b, c) = triplet_with_sum(1000).unwrap();
println!("a = {a}, b = {b}, c = {c} // abc = {}", a * b * c);
}