Skip to content

Commit

Permalink
solve p0006 in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 18, 2024
1 parent 34abf3d commit 8a58d06
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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| |
+-------------------------------------------+---------------+
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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| |
+-----------+------+------+------+------+------+
Expand Down
8 changes: 4 additions & 4 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
];
Expand All @@ -40,7 +40,7 @@ fn main() {
}

#[cfg(test)]
seq!(N in 0..7 {
seq!(N in 0..8 {
#[rstest]
#[timeout(Duration::new(60, 0))]
#(
Expand Down
28 changes: 28 additions & 0 deletions rust/src/p0006.rs
Original file line number Diff line number Diff line change
@@ -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;
}

0 comments on commit 8a58d06

Please sign in to comment.