Hey I am having problem with problem 34 from project euler I get 145 as the total of all numbers that are also the sum of their own digit's factorials. (145 = 1! + 4! + 5!) It is quite funny that 145 was the example from the website :p Is the numbers just incredibly big?
#include "stdafx.h"
#include <iostream>
usingnamespace std;
int Factorial(int n);
int actions(int n);
int main()
{
int MAX = 1000000;
int sum = 0;
for (int i = 10; i < MAX; i++)
sum += actions(i);
cout << sum;
cin.get();
return 0;
}
int Factorial(int n)
{
if (n > 1)
{
n = (n+1) * Factorial(--n);
}
return n;
}
int actions(int n)
{
int copy = n;
int tot = 0;
while(n > 10)
{
tot += Factorial(n % 10);
n /= 10;
}
tot += Factorial(n % 10);
if(copy == tot)
return tot;
elsereturn 0;
}
Borgound Aries, that produces the exact same output as my code did :/ Is my problem in the function that gets digits one by one? JLBorges thanks for the code but I don't really undestand it and it is not "my" code so I don't want to use it. I just want some help pointing out what is wrong and a possible solution.
JLBorges thanks for the code but I don't really undestand it and it is not "my" code so I don't want to use it. I just want some help pointing out what is wrong and a possible solution
"Here's a solution."
"That's not my code. I don't want to use that. Can anyone give me a solution?"
Mmhmm. Maybe you should make the effort to at least understand what is happening in the code so you can write your own version.
This is a very simple problem. The solution is also rather simple. It shouldn't be that hard to break down 24 lines of code.
Otherwise, you might want to work out the correct value for some test cases and see how the output of those test cases differ with your code. A very large part of writing code is being able to figure out what you did wrong. You might begin by seeing what value your factorial function returns for 2!