From 863f051f7ddbb871a50af0285b796b5531817cd0 Mon Sep 17 00:00:00 2001 From: Philippe Zwietering Date: Mon, 26 Jun 2023 15:38:35 +0200 Subject: [PATCH] Finished PE 091, fairly straightforward, just brute-forced --- projecteuler/091/Cargo.toml | 8 ++++++++ projecteuler/091/src/main.rs | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 projecteuler/091/Cargo.toml create mode 100644 projecteuler/091/src/main.rs diff --git a/projecteuler/091/Cargo.toml b/projecteuler/091/Cargo.toml new file mode 100644 index 0000000..fca4e93 --- /dev/null +++ b/projecteuler/091/Cargo.toml @@ -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] diff --git a/projecteuler/091/src/main.rs b/projecteuler/091/src/main.rs new file mode 100644 index 0000000..85a7170 --- /dev/null +++ b/projecteuler/091/src/main.rs @@ -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); +}