Finished projecteuler 086 with ugly solution but hey it works
This commit is contained in:
7
projecteuler/086/Cargo.lock
generated
Normal file
7
projecteuler/086/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
8
projecteuler/086/Cargo.toml
Normal file
8
projecteuler/086/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "main"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
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