-
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.
Solve p0034, make math functions more generic
- Loading branch information
1 parent
3b3579d
commit e0368a5
Showing
9 changed files
with
80 additions
and
12 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
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 34 | ||
================================= | ||
|
||
View source code `here on GitHub! <https://github.com/LivInTheLookingGlass/Euler/blob/master/rust/src/p0034.rs>`_ | ||
|
||
Includes | ||
-------- | ||
|
||
- `math <./math.html>`_ | ||
|
||
Problem Solution | ||
---------------- | ||
|
||
.. rust:fn:: p0034::p0034() -> i128 | ||
.. literalinclude:: ../../rust/src/p0034.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,32 @@ | ||
/* | ||
Project Euler Problem 34 | ||
This ended up being a filtering problem. The problem with my solution is that I | ||
am not satisfied with my filter at all. I feel like there is a more efficient | ||
way to go about it. | ||
Problem: | ||
145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. | ||
Find the sum of all numbers which are equal to the sum of the factorial of | ||
their digits. | ||
Note: as 1! = 1 and 2! = 2 are not sums they are not included. | ||
*/ | ||
use crate::math::factorial; | ||
|
||
pub fn p0034() -> i128 { | ||
let mut answer: u32 = 0; | ||
for x in 10..100000 { | ||
let string: String = x.to_string(); | ||
let sum = | ||
string.bytes() | ||
.into_iter() | ||
.fold(0, |a, b| a + factorial::<u32>(b - '0' as u8)); | ||
if sum == x { | ||
answer += x; | ||
} | ||
} | ||
return answer.into(); | ||
} |