From 8a58d06dbdd23db4a53685bbeb7902eb5e353467 Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Wed, 17 Jul 2024 21:43:42 -0500 Subject: [PATCH] solve p0006 in rust --- README.rst | 2 +- docs/index.rst | 2 +- rust/src/main.rs | 8 ++++---- rust/src/p0006.rs | 28 ++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 rust/src/p0006.rs diff --git a/README.rst b/README.rst index 8624192a..ca54121c 100644 --- a/README.rst +++ b/README.rst @@ -41,7 +41,7 @@ LivInTheLookingGlass’s Project Euler solutions | | Pypy 3.6+ |br| | | | | | GraalPy 23.1+ | | | +------------+---------------------+--------+---------------+ -| Rust | 1.69+ | 7 | |Rust| | +| Rust | 1.69+ | 8 | |Rust| | +------------+---------------------+--------+---------------+ | Documentation (in progress) | |Pages| | +-------------------------------------------+---------------+ diff --git a/docs/index.rst b/docs/index.rst index 22464b58..ef0065c8 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -106,7 +106,7 @@ This project is divided into several Makefiles, connected by a root Makefile whi +-----------+------+------+------+------+------+ |:prob:`5` | |d| | | | |d| | |d| | +-----------+------+------+------+------+------+ -|:prob:`6` | |d| | | | |d| | | +|:prob:`6` | |d| | | | |d| | |d| | +-----------+------+------+------+------+------+ |:prob:`7` | |d| | | | |d| | |d| | +-----------+------+------+------+------+------+ diff --git a/rust/src/main.rs b/rust/src/main.rs index 53aa4d94..03105e4a 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -9,21 +9,21 @@ use rstest::rstest; #[cfg(test)] use itertools::Itertools; -seq!(N in 0001..=0005 { +seq!(N in 0001..=0007 { mod p~N; }); -mod p0007; mod p0010; mod primes; type ProblemType = fn() -> u64; type ProblemRef<'a> = (&'a str, ProblemType, u64); -const ANSWERS: [ProblemRef; 7] = [ +const ANSWERS: [ProblemRef; 8] = [ ("p0001", p0001::p0001, 233168), ("p0002", p0002::p0002, 4613732), ("p0003", p0003::p0003, 6857), ("p0004", p0004::p0004, 906609), ("p0005", p0005::p0005, 232792560), + ("p0006", p0006::p0006, 25164150), ("p0007", p0007::p0007, 104743), ("p0010", p0010::p0010, 142913828922), ]; @@ -40,7 +40,7 @@ fn main() { } #[cfg(test)] -seq!(N in 0..7 { +seq!(N in 0..8 { #[rstest] #[timeout(Duration::new(60, 0))] #( diff --git a/rust/src/p0006.rs b/rust/src/p0006.rs new file mode 100644 index 00000000..adc4d04a --- /dev/null +++ b/rust/src/p0006.rs @@ -0,0 +1,28 @@ +/* +Project Euler Problem 6 + +This turned out to be really easy + +Problem: + +The sum of the squares of the first ten natural numbers is, +1**2 + 2**2 + ... + 10**2 = 385 + +The square of the sum of the first ten natural numbers is, +(1 + 2 + ... + 10)**2 = 55**2 = 3025 + +Hence the difference between the sum of the squares of the first ten natural +numbers and the square of the sum is 3025 − 385 = 2640. + +Find the difference between the sum of the squares of the first one hundred +natural numbers and the square of the sum. +*/ + + +pub fn p0006() -> u64 { + let group = 1..101; + let sum_of_squares = group.clone().fold(0, |x, y| x + y * y); + let sum: u64 = group.sum(); + let square_of_sum = sum * sum; + return square_of_sum - sum_of_squares; +}