Finished projecteuler 086 with ugly solution but hey it works
This commit is contained in:
52
projecteuler/086/src/main.rs
Normal file
52
projecteuler/086/src/main.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
use std::time::Instant;
|
||||
|
||||
fn does_cuboid_have_integer_shortest_path(a: u64, b: u64, c: u64) -> bool {
|
||||
assert!(a >= b);
|
||||
assert!(b >= c);
|
||||
|
||||
let d = a * a + (b + c).pow(2);
|
||||
let root = (d as f64).sqrt() as u64;
|
||||
|
||||
d == root * root
|
||||
}
|
||||
|
||||
fn new_solution(old_solution: u64, max_m: u64) -> u64 {
|
||||
let mut current_number_of_solutions = 0;
|
||||
|
||||
for h in 1..=max_m {
|
||||
for w in 1..=h {
|
||||
if does_cuboid_have_integer_shortest_path(max_m, h, w) {
|
||||
current_number_of_solutions += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
current_number_of_solutions + old_solution
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, this is Patrick!");
|
||||
let now = Instant::now();
|
||||
|
||||
let max_value = 1000000;
|
||||
|
||||
let mut number_of_solutions = vec![0];
|
||||
let mut m = 1;
|
||||
|
||||
while let Some(&n) = number_of_solutions.last() {
|
||||
if n > max_value {
|
||||
break;
|
||||
}
|
||||
|
||||
number_of_solutions.push(new_solution(n, m));
|
||||
m += 1;
|
||||
}
|
||||
|
||||
println!(
|
||||
"The least value of M such that the number of solutions first exceeds {} is: {}",
|
||||
max_value,
|
||||
number_of_solutions.len() - 1,
|
||||
);
|
||||
|
||||
println!("Time passed: {:?}", Instant::now() - now);
|
||||
}
|
||||
Reference in New Issue
Block a user