Skip to content

Commit

Permalink
solve p0015 in rust, update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 23, 2024
1 parent f3c570b commit 9490a04
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ LivInTheLookingGlass’s Project Euler solutions
| | Pypy 3.6+ |br| | | |
| | GraalPy 23.1+ | | |
+------------+-------------------------+--------+-------------------+
| Rust | 1.69+ | 12 | |Rust| |
| Rust | 1.69+ | 13 | |Rust| |
+------------+-------------------------+--------+-------------------+
| Documentation (in progress) | |Pages| |
+-----------------------------------------------+-------------------+
Expand Down
9 changes: 8 additions & 1 deletion cplusplus/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,11 @@ Problems Solved

- ☒ `1 <./p0001.cpp>`__
- ☒ `2 <./p0002.cpp>`__

- ☒ `4 <./p0004.cpp>`__
- ☒ `6 <./p0006.cpp>`__
- ☒ `9 <./p0009.cpp>`__
- ☒ `11 <./p0011.cpp>`__
- ☒ `14 <./p0014.cpp>`__
- ☒ `15 <./p0015.cpp>`__
- ☒ `34 <./p0034.cpp>`__
- ☒ `76 <./p0076.cpp>`__
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ This project is divided into several Makefiles, connected by a root Makefile whi
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`14` |:c-d:`0014` |:cp-d:`0014`| | |:py-d:`0014`| |
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`15` |:c-d:`0015` |:cp-d:`0015`| | |:py-d:`0015`| |
|:prob:`15` |:c-d:`0015` |:cp-d:`0015`| | |:py-d:`0015`|:rs-d:`0015`|
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`16` |:c-d:`0016` | | | |:py-d:`0016`| |
+-----------+------------+------------+------------+------------+------------+------------+
Expand Down
12 changes: 12 additions & 0 deletions docs/rust/math.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
math.rs
=======

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/rust/src/math.rs>`_

.. rust:fn:: math::n_choose_r(n: usize, r: usize) -> i128
Returns the number of ways to choose r items from a set of n.

.. literalinclude:: ../../rust/src/math.rs
:language: rust
:linenos:
18 changes: 18 additions & 0 deletions docs/rust/p0015.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Rust Implementation of Problem 15
=================================

View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/rust/src/p0015.rs>`_

Includes
--------

- `primes <./math.html>`_

Problem Solution
----------------

.. rust:fn:: p0015::p0015() -> i128
.. literalinclude:: ../../rust/src/p0015.rs
:language: rust
:linenos:
7 changes: 4 additions & 3 deletions javascript/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Problems Solved

- ☒ `1 <./p0001.js>`__
- ☒ `2 <./p0002.js>`__
- ☒ `6 <./p0002.js>`__
- ☒ `8 <./p0002.js>`__
- ☒ `9 <./p0002.js>`__
- ☒ `4 <./p0004.js>`__
- ☒ `6 <./p0006.js>`__
- ☒ `8 <./p0008.js>`__
- ☒ `9 <./p0009.js>`__
3 changes: 3 additions & 0 deletions rust/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,7 @@ Problems Solved
- ☒ `8 <./src/p0008.rs>`__
- ☒ `9 <./src/p0009.rs>`__
- ☒ `10 <./src/p0010.rs>`__
- ☒ `11 <./src/p0011.rs>`__
- ☒ `15 <./src/p0015.rs>`__
- ☒ `76 <./src/p0076.rs>`__

7 changes: 5 additions & 2 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ use itertools::Itertools;
seq!(N in 0001..=0011 {
mod p~N;
});
mod p0015;
mod p0076;
mod math;
mod primes;

type ProblemType = fn() -> i128;
type ProblemRef<'a> = (&'a str, ProblemType, i128);
const ANSWERS: [ProblemRef; 12] = [
const ANSWERS: [ProblemRef; 13] = [
("p0001", p0001::p0001, 233168),
("p0002", p0002::p0002, 4613732),
("p0003", p0003::p0003, 6857),
Expand All @@ -29,6 +31,7 @@ const ANSWERS: [ProblemRef; 12] = [
("p0009", p0009::p0009, 31875000),
("p0010", p0010::p0010, 142913828922),
("p0011", p0011::p0011, 70600674),
("p0015", p0015::p0015, 137846528820),
("p0076", p0076::p0076, 190569291),
];

Expand All @@ -44,7 +47,7 @@ fn main() {
}

#[cfg(test)]
seq!(N in 0..12 {
seq!(N in 0..13 {
#[rstest]
#[timeout(Duration::new(60, 0))]
#(
Expand Down
57 changes: 57 additions & 0 deletions rust/src/math.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
pub fn n_choose_r(n: usize, r: usize) -> i128 {
// slow path for larger numbers
let mut answer: i128 = 1;
let mut tmp: i128;
let mut factors: Vec<i8> = vec![0; n + 1];
// collect factors of final number
for i in 2..=n {

Check warning

Code scanning / clippy

the loop variable i is only used to index factors Warning

the loop variable i is only used to index factors
factors[i] = 1;
}
// negative factor values indicate need to divide
for i in 2..=r {

Check warning

Code scanning / clippy

the loop variable i is only used to index factors Warning

the loop variable i is only used to index factors
factors[i] -= 1;
}
for i in 2..=(n - r) {

Check warning

Code scanning / clippy

the loop variable i is only used to index factors Warning

the loop variable i is only used to index factors
factors[i] -= 1;
}
// this loop reduces to prime factors only
for i in (1..n).rev() {
for j in 2..i {
if i % j == 0 {
factors[j] += factors[i];
factors[i / j] += factors[i];
factors[i] = 0;
break;
}
}
}
let mut i: usize = 2;
let mut j: usize = 2;
while i <= n {
while factors[i] > 0 {
tmp = answer;
answer *= i as i128;
while answer < tmp && j <= n {
while factors[j] < 0 {
tmp /= j as i128;
factors[j] += 1;
}
j += 1;
answer = tmp * i as i128;
}
if answer < tmp {
return -1; // this indicates an overflow
}
factors[i] -= 1;
}
i += 1;
}
while j <= n {
while factors[j] < 0 {
answer /= j as i128;
factors[j] += 1;
}
j += 1;
}
return answer;
}
17 changes: 17 additions & 0 deletions rust/src/p0015.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
Project Euler Problem 15
This one was also relatively easy, especially given the work I have
done on my prime number infrastructure.
Problem:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
*/
use crate::math;

pub fn p0015() -> i128 {
return math::n_choose_r(40, 20).into();
}

0 comments on commit 9490a04

Please sign in to comment.