Finished PE 94, a tale of floats and integers
This commit is contained in:
8
projecteuler/094/Cargo.toml
Normal file
8
projecteuler/094/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]
|
||||||
47
projecteuler/094/src/main.rs
Normal file
47
projecteuler/094/src/main.rs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
fn check_triangle(two_sides: u64, one_side: u64) -> bool {
|
||||||
|
let c = 4 * two_sides * two_sides - one_side * one_side;
|
||||||
|
let r = (c as f64).sqrt() as u64;
|
||||||
|
|
||||||
|
// println!("{} {}", two_sides, r);
|
||||||
|
return r * r == c && r > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, this is Patrick!");
|
||||||
|
let now = Instant::now();
|
||||||
|
|
||||||
|
const MAX_PERIMETER: u64 = 1000000000;
|
||||||
|
|
||||||
|
let mut perimeter_sum = 0;
|
||||||
|
|
||||||
|
let mut i = 1;
|
||||||
|
loop {
|
||||||
|
if 3 * i - 1 > MAX_PERIMETER {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// First check the case where the single side is equal to i - 1
|
||||||
|
if i > 1 && check_triangle(i, i - 1) {
|
||||||
|
perimeter_sum += 3 * i - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then check the case where the single side is equal to i + 1
|
||||||
|
if 3 * i + 1 <= MAX_PERIMETER && check_triangle(i, i + 1) {
|
||||||
|
perimeter_sum += 3 * i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"Sum of perimeters of almost equilateral triangles with integral side is: {}",
|
||||||
|
perimeter_sum
|
||||||
|
);
|
||||||
|
println!("Time passed: {:?}", Instant::now() - now);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn check() {
|
||||||
|
assert!(check_triangle(5, 6));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user