Last active
February 11, 2019 17:10
-
-
Save bantic/34af3a0888cdd7b24a952c252aa4876a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashSet; | |
pub fn find(sum: u32) -> HashSet<[u32; 3]> { | |
(2..(sum / 2)) | |
.map(|a| { | |
let b_plus_c = sum - a; | |
let b_numerator = b_plus_c.pow(2) - a.pow(2); | |
let b_denominator = 2 * b_plus_c; | |
let b = b_numerator / b_denominator; | |
let c = b_plus_c - b; | |
([a, b, c], b_numerator % b_denominator) | |
}) | |
.filter(|(_, remainder)| remainder == &0) | |
.map(|(triplet, _)| triplet) | |
.collect() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment