Finished PE 94, a tale of floats and integers

This commit is contained in:
2023-06-27 13:29:55 +02:00
parent 958ee4651e
commit 419269d0a0
2 changed files with 55 additions and 0 deletions

View 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]

View 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));
}