-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3c570b
commit 9490a04
Showing
10 changed files
with
126 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |