add 94 (WIP)
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "euler94"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -0,0 +1,50 @@
|
||||
type Num = u64;
|
||||
|
||||
const ONE_BILLION: Num = 1_000_000_000;
|
||||
|
||||
fn main() {
|
||||
let mut sum = 0 as Num;
|
||||
'outer: for i in 2.. {
|
||||
let a_b = i as Num;
|
||||
for c in [a_b - 1, a_b + 1] {
|
||||
let t = Triangle::new(a_b, c);
|
||||
let p = t.perimeter();
|
||||
|
||||
if p > ONE_BILLION {
|
||||
break 'outer;
|
||||
}
|
||||
|
||||
if t.has_integer_area() {
|
||||
sum += p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("{sum}");
|
||||
}
|
||||
|
||||
struct Triangle {
|
||||
a_b: Num,
|
||||
c: Num,
|
||||
}
|
||||
|
||||
impl Triangle {
|
||||
fn new(a_b: Num, c: Num) -> Self {
|
||||
Self { a_b, c }
|
||||
}
|
||||
|
||||
fn perimeter(&self) -> Num {
|
||||
2 * self.a_b + self.c
|
||||
}
|
||||
|
||||
fn has_integer_area(&self) -> bool {
|
||||
let a_b = self.a_b as f64;
|
||||
let c = self.c as f64;
|
||||
let area = c / 4. * (4. * a_b.powi(2) - c.powi(2)).sqrt();
|
||||
is_integer(area)
|
||||
}
|
||||
}
|
||||
|
||||
fn is_integer(x: f64) -> bool {
|
||||
(x.round() - x).abs() <= f64::EPSILON
|
||||
}
|
||||
Reference in New Issue
Block a user