Finished PE 091, fairly straightforward, just brute-forced
This commit is contained in:
40
projecteuler/091/src/main.rs
Normal file
40
projecteuler/091/src/main.rs
Normal file
@@ -0,0 +1,40 @@
|
||||
use std::collections::HashSet;
|
||||
use std::time::Instant;
|
||||
|
||||
// Als het goed is, is dit te brute-forcen. Er zijn namelijk 50^4 permutaties van coordinaten,
|
||||
// want ieder coordinaat is twee cijfers (x,y) met 0 < x, y <= 50. Dit blijft in de miljoenen
|
||||
// hangen dus gegeven een O(1) check kun je dat best snel uitrekenen zonder slim te doen.
|
||||
|
||||
fn check_right_triangle(a: &(u32, u32), b: &(u32, u32)) -> bool {
|
||||
let po = a.0 * a.0 + a.1 * a.1;
|
||||
let qo = b.0 * b.0 + b.1 * b.1;
|
||||
let pq = (a.0 - b.0).pow(2) + (a.1 - b.1).pow(2);
|
||||
|
||||
let mut lengths = vec![po, qo, pq];
|
||||
lengths.sort();
|
||||
|
||||
return lengths[0] + lengths[1] == lengths[2] && a.0 * b.1 != a.1 * b.0;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, this is Patrick!");
|
||||
let now = Instant::now();
|
||||
|
||||
const COORD_MAX: u32 = 50;
|
||||
|
||||
let mut found_coordinates = HashSet::new();
|
||||
for a_x in 0..=COORD_MAX {
|
||||
for a_y in 0..=COORD_MAX {
|
||||
for b_x in a_x..=COORD_MAX {
|
||||
for b_y in 0..=a_y {
|
||||
if check_right_triangle(&(a_x, a_y), &(b_x, b_y)) {
|
||||
found_coordinates.insert(((a_x, a_y), (b_x, b_y)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
println!("Number of triangles: {}", found_coordinates.len());
|
||||
println!("Time passed: {:?}", Instant::now() - now);
|
||||
}
|
||||
Reference in New Issue
Block a user