problem 34, proj euler

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include "stdafx.h"
#include <iostream>
using namespace 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;
	else
		return 0;
}
I believe your factorial function is wrong. My calculator seems to agree... This should work.

1
2
3
4
constexpr int Factorial(int n)
{
	return n * (n < 2 ? 1 : Factorial(n - 1));
}
Memoize the computation of factorials.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

unsigned int factorial[10] = { 1 } ;

unsigned int sum_factorial_of_digits( unsigned int n )
{
    if( n < 10 ) return factorial[n] ;
    else return factorial[n%10] + sum_factorial_of_digits(n/10) ;
}

bool equal_to_sum_factorial_of_digits( unsigned int n )
{ return n == sum_factorial_of_digits(n) ; }

int main()
{
    for( int i = 1 ; i < 10 ; ++i ) factorial[i] = i * factorial[i-1] ;

    constexpr unsigned int MAX = 1000000 ;
    unsigned long long sum = 0 ;
    for( unsigned int n = 10 ; n <= MAX ; ++n )
        if( equal_to_sum_factorial_of_digits(n) ) sum += n ;

    std::cout << sum << '\n' ;
}
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.
@Borgound Aries: 0! is 1
@OP: n = (n+1) * Factorial(--n); is undefined behaviour. (¿where did you get that definition?)
Last edited on
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!

Registered users can post here. Sign in or register to post.