Created
August 3, 2017 11:25
-
-
Save Shaddy/2b4465aec9d1225ae996bff9d72af56c to your computer and use it in GitHub Desktop.
Pythagoras problem
This file contains 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
#![feature(conservative_impl_trait)] // this requires nigthly version | |
#[macro_use] | |
extern crate itertools; | |
#[derive(Debug)] | |
struct Triangle { | |
a: u32, | |
b: u32, | |
c: u32, | |
} | |
impl Triangle { | |
fn is_pythagoric(&self) -> bool { | |
self.a.pow(2) + self.b.pow(2) == self.c.pow(2) | |
} | |
fn product(&self) -> u32 { | |
self.a * self.b * self.c | |
} | |
fn sum(&self) -> u32 { | |
self.a + self.b + self.c | |
} | |
fn generate(max: u32) -> impl Iterator<Item = Triangle> { | |
(1..max).flat_map(move |x| (x..max).flat_map(move |y| (max-x+y..max).map(move |z| Triangle{a: x, b: y, c: z}))) | |
} | |
} | |
fn main() { | |
for triangle in Triangle::generate(100) { | |
println!("{:#?}", triangle); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment