Created
January 7, 2019 08:46
-
-
Save 44100hertz/294c2c229639206d0585185e911f4a33 to your computer and use it in GitHub Desktop.
Ugly solution to this: https://atilanevesoncode.wordpress.com/2018/12/31/comparing-pythagorean-triples-in-c-d-and-rust/
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
fn triples() -> impl Iterator<Item=(u32, u32, u32)> { | |
let (mut x, mut y, mut z) = (1, 1, 1); | |
(0..).map(move |_| { | |
loop { | |
y += 1; | |
if y > z { | |
x += 1; | |
y = x; | |
} | |
if x > z { | |
z += 1; | |
x = 1; | |
y = 1; | |
} | |
if x*x + y*y == z*z { | |
return (x, y, z); | |
} | |
} | |
}) | |
} | |
fn main() { | |
for (x, y, z) in triples().take(1000) { | |
println!("({}, {}, {})", x, y, z); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment