Skip to content

Commit

Permalink
Solve p0012 in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
LivInTheLookingGlass committed Jul 23, 2024
1 parent 9490a04 commit aafa3b2
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 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+ | 13 | |Rust| |
| Rust | 1.69+ | 14 | |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 @@ -126,7 +126,7 @@ This project is divided into several Makefiles, connected by a root Makefile whi
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`11` |:c-d:`0011` |:cp-d:`0011`| | |:py-d:`0011`|:rs-d:`0011`|
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`12` |:c-i:`0012` | | | |:py-d:`0012`| |
|:prob:`12` |:c-i:`0012` | | | |:py-d:`0012`|:rs-d:`0012`|
+-----------+------------+------------+------------+------------+------------+------------+
|:prob:`13` |:c-d:`0013` | | | |:py-d:`0013`| |
+-----------+------------+------------+------------+------------+------------+------------+
Expand Down
18 changes: 18 additions & 0 deletions docs/rust/p0012.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Rust Implementation of Problem 12
=================================

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

Includes
--------

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

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

.. rust:fn:: p0012::p0012() -> i128
.. literalinclude:: ../../rust/src/p0012.rs
:language: rust
:linenos:
1 change: 1 addition & 0 deletions rust/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Problems Solved
- ☒ `9 <./src/p0009.rs>`__
- ☒ `10 <./src/p0010.rs>`__
- ☒ `11 <./src/p0011.rs>`__
- ☒ `12 <./src/p0012.rs>`__
- ☒ `15 <./src/p0015.rs>`__
- ☒ `76 <./src/p0076.rs>`__

5 changes: 3 additions & 2 deletions rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rstest::rstest;
#[cfg(test)]
use itertools::Itertools;

seq!(N in 0001..=0011 {
seq!(N in 0001..=0012 {
mod p~N;
});
mod p0015;
Expand All @@ -19,7 +19,7 @@ mod primes;

type ProblemType = fn() -> i128;
type ProblemRef<'a> = (&'a str, ProblemType, i128);
const ANSWERS: [ProblemRef; 13] = [
const ANSWERS: [ProblemRef; 14] = [
("p0001", p0001::p0001, 233168),
("p0002", p0002::p0002, 4613732),
("p0003", p0003::p0003, 6857),
Expand All @@ -31,6 +31,7 @@ const ANSWERS: [ProblemRef; 13] = [
("p0009", p0009::p0009, 31875000),
("p0010", p0010::p0010, 142913828922),
("p0011", p0011::p0011, 70600674),
("p0012", p0012::p0012, 76576500),
("p0015", p0015::p0015, 137846528820),
("p0076", p0076::p0076, 190569291),
];
Expand Down
41 changes: 41 additions & 0 deletions rust/src/p0012.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Project Euler Problem 12
I'm very glad that I did the most expansive version of my prime infrastructure
from the start.
Problem:
The sequence of triangle numbers is generated by adding the natural numbers. So
the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten
terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
Let us list the factors of the first seven triangle numbers:
1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28
We can see that 28 is the first triangle number to have over five divisors.
What is the value of the first triangle number to have over five hundred
divisors?
*/
use crate::primes;

pub fn p0012() -> i128 {
let mut num: u64 = 0;
for x in 1.. {
num += x;
if proper_divisors(num).len() > 499 {
return num.into();
}
}
return -1;
}

0 comments on commit aafa3b2

Please sign in to comment.