Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sum of factorials #101

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

maretekent
Copy link

@maretekent maretekent commented Sep 4, 2018

Solution Number: {34.rs}

Checklist:

  • Created binary in src/bin
  • Written Documentation
  • Written Tests

What

Find the sum of all numbers which are equal to the sum of the factorial of their digits.
145 is an interesting number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
Note: as 1! = 1 and 2! = 2 are not sums they are not included.

How

  • Read the integers in the range between 10 and 10000
  • for every integer break it down and read them like characters with each of them getting the factorial
  • then sum the factorials
  • When you get the sum compare if its equal to the initial integer before breaking it down

Solution

screen shot 2018-09-11 at 17 48 12

Nice to have:

I would have loved to iterate over the characters of string and convert and add them using fold

Challenges

  1. Rust is statically typed language. It expects types. I found out that the fibonacci of huge numbers I could easily get the error You are attempting to multiply with overflow
  2. Comparing the resulting factorial with the number - Parsing the number
  3. Writing its test -- perhaps I should have created a function of the entire content inside main
  4. No nice rust data structures or techniques used here. I would like to see other people thoughful implementations

Ref

Project Euler problem 34
Issue 34

Copy link
Contributor

@mattgathu mattgathu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a binary file: src/bin/2 that is not required.

Your solution does not test the base case provided:
1! + 4! + 5! = 1 + 24 + 120 = 145.. Add a test for this base case.

Also you need to solve the main question:
Find the sum of all numbers which are equal to the sum of the factorial of their digits.

@mattgathu
Copy link
Contributor

The answer to Find the sum of all numbers which are equal to the sum of the factorial of their digits. is 40730

screen shot 2018-09-05 at 10 14 29

@mattgathu
Copy link
Contributor

mattgathu commented Sep 13, 2018

@maretekent maybe we need a test for 1! + 4! + 5! = 1 + 24 + 120 = 145.

A function that takes the number e.g.145 calculates the factorial of its digits and returns their sum e.g.145.

The test for this function will be something like this:

#[test]
fn test_factorial_sum(){
    assert_eq!(factorial_sum(145), 145);
}

We can extrapolate this to an algorithm for finding the sum of all these numbers:

  • create a vector to hold numbers
  • for each number in the range 0..10K
  • if factorial_sum(number) == number, add number to vector
  • assert the sum of the vector is 40730 (the expected answer)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants