Skip to content

Instantly share code, notes, and snippets.

@bantic
Last active February 11, 2019 17:10
Show Gist options
  • Save bantic/34af3a0888cdd7b24a952c252aa4876a to your computer and use it in GitHub Desktop.
Save bantic/34af3a0888cdd7b24a952c252aa4876a to your computer and use it in GitHub Desktop.
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