Skip to content

Instantly share code, notes, and snippets.

@Shaddy
Created August 3, 2017 11:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shaddy/2b4465aec9d1225ae996bff9d72af56c to your computer and use it in GitHub Desktop.
Save Shaddy/2b4465aec9d1225ae996bff9d72af56c to your computer and use it in GitHub Desktop.
Pythagoras problem
#![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