add 85 and 86
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "euler86"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[profile.release]
|
||||
lto = "fat"
|
||||
debug = true
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
use std::f64::EPSILON;
|
||||
|
||||
const TARGET: u32 = 1_000_000;
|
||||
|
||||
fn main() {
|
||||
let mut m = 1;
|
||||
let mut distinct = 0;
|
||||
|
||||
loop {
|
||||
println!("trying: {m}");
|
||||
|
||||
for x in 1..m + 1 {
|
||||
for y in x..m + 1 {
|
||||
let z = m;
|
||||
let c = Cuboid::new(x, y, z);
|
||||
if c.shortest_path_is_integer() {
|
||||
distinct += 1;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if distinct > TARGET {
|
||||
break;
|
||||
}
|
||||
|
||||
m += 1;
|
||||
}
|
||||
|
||||
println!("\n{m}");
|
||||
}
|
||||
|
||||
struct Cuboid {
|
||||
x: u32,
|
||||
y: u32,
|
||||
z: u32,
|
||||
}
|
||||
|
||||
impl Cuboid {
|
||||
fn new(x: u32, y: u32, z: u32) -> Self {
|
||||
Self { x, y, z }
|
||||
}
|
||||
|
||||
fn shortest_path_is_integer(&self) -> bool {
|
||||
// because of the way we iterator over x, y and z, these are guaranteed to be in order
|
||||
// and therefore produces the shortest path
|
||||
let shortest_path = pythagorean(self.x + self.y, self.z);
|
||||
|
||||
is_integer(shortest_path)
|
||||
}
|
||||
}
|
||||
|
||||
fn pythagorean(x: u32, y: u32) -> f64 {
|
||||
((x.pow(2) + y.pow(2)) as f64).sqrt()
|
||||
}
|
||||
|
||||
fn is_integer(result: f64) -> bool {
|
||||
let rounded = result.round();
|
||||
|
||||
(result - rounded).abs() <= EPSILON
|
||||
}
|
||||
Reference in New Issue
Block a user