39
This commit is contained in:
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "euler39"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
debug = true
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
#[derive(Debug, Hash)]
|
||||||
|
pub struct Triangle {
|
||||||
|
a: usize,
|
||||||
|
b: usize,
|
||||||
|
c: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Triangle {
|
||||||
|
pub fn new(a: usize, b: usize, c: usize) -> Option<Self> {
|
||||||
|
Self { a, b, c }.sanity_check()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sanity_check(self) -> Option<Self> {
|
||||||
|
if !self.sides_are_sufficiently_long() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
if !self.pythagoras_holds() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
Some(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sides_are_sufficiently_long(&self) -> bool {
|
||||||
|
self.a + self.b > self.c || self.a + self.c > self.b || self.b + self.c > self.a
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pythagoras_holds(&self) -> bool {
|
||||||
|
self.a.pow(2) + self.b.pow(2) == self.c.pow(2)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use euler39::Triangle;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut vec = vec![];
|
||||||
|
for p in 2..=1_000_usize {
|
||||||
|
vec.push(0);
|
||||||
|
let current = vec.get_mut(p - 2).unwrap();
|
||||||
|
for a in 2..=p {
|
||||||
|
let remainder = p - a;
|
||||||
|
let limit = remainder / 2;
|
||||||
|
for b in 1..limit {
|
||||||
|
let c = remainder - b;
|
||||||
|
if let Some(_triangle) = Triangle::new(a, b, c) {
|
||||||
|
*current += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let result = vec
|
||||||
|
.iter()
|
||||||
|
.enumerate()
|
||||||
|
.max_by_key(|(_, value)| **value)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
println!("{}: {}", result.0 + 2, result.1)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user