problem 34, proj euler

closed account (ETAkoG1T)
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' ;
}
closed account (ETAkoG1T)
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!

closed account (ETAkoG1T)
Cire, obviously I am not interested in a solution to the project euler problem, why would I post my code then? (or maybe it wasn't that obvious, my mistake then)
In addition to this code of the solution is easy to find online, all I needed was a hint pointing out what is wrong with the code.

I have found the problem. The only two problems was that my MAX variable was too small and 0! = 1 not 0! = 0.

Mmhmm. Maybe you should make the effort to at least understand what is happening in the code so you can write your own version.

Seriously? I posted code of my program in the first post. What would the point of making a new version of his program be when I have my own program? Anyways I have solved the problem a while ago, was just hoping I would get a simple answer from someone that could find the problem in my code.
Last edited on
closed account (ETAkoG1T)
ne555:
What do you mean? It is working for me :P
I have found the problem. The only two problems was that my MAX variable was too small and 0! = 1 not 0! = 0.

As ne555 pointed out your factorial function was wrong and evinced undefined behavior.


Seriously? I posted code of my program in the first post. What would the point of making a new version of his program be when I have my own program?

Seriously? You can't see the point in analyzing working code to see how it differs from non-working code? Seriously? You can't see how refactoring code might improve your understanding of the problem?

Seriously?
closed account (ETAkoG1T)
I knew you was going to reply something like that. In this situation though (to solve my problem) there is better ways to solve the problem. If ne555 had pointed out that 0! = 1 before I had found this out myself that would have solved the problem.

Analyzing can really help improving but it won't help me fix my program unless they use the same method for finding the answer.

As ne555 pointed out your factorial function was wrong and evinced undefined behavior.

Is undefined behaviour an error because I never got any errors. This really wasn't all that complicated and I shouldn't have to make a new program inspired by someone else's code just because I didn't know that the factorial of !0 is 1 and that I had to check higher numbers. Anyways the program works good for me now, I made an array to hold the factorials from 0-9 but it wasn't necessary.

You can't see how refactoring code might improve your understanding of the problem?

It looks like you think I didn't understand the problem, when in reality my code was almost complete when I posted it.
Last edited on
> Is undefined behaviour an error because I never got any errors.
¿was your program correct then?
undefined behaviour is undefined, try to avoid it.
closed account (ETAkoG1T)
Yep my program was correct, also I don't think it gave me any warnings either. But thanks for noting it, did you see anything wrong with it? (except for the fact that 0! gets the value 0 instead of 1 but that is an easy fix) I like recursion and try to use it as much as I can :)
This really wasn't all that complicated and I shouldn't have to make a new program inspired by someone else's code just because I didn't know that the factorial of !0 is 1 and that I had to check higher numbers.

Nobody implied that you should have to. My problem is you summarily dismissing help because it wasn't in the form that was most convenient to you. It doesn't inspire. Had you inspected said code you might've realized that !0 is 1.

"Hey! Debug my program for me! Don't supply any examples though! I refuse to learn from that stuff!"

No thanks.


Is undefined behaviour an error because I never got any errors.

Undefined behavior is the result of incorrect code. Compilers are not required to diagnose it. The fact that the program works is incidental. It may not work with a different compiler or a different version of the same compiler or even the same version of the same compiler using different settings.
closed account (ETAkoG1T)
Generally I would agree that learning from reading other's code is good, but I would consider that cheating when it comes to project euler, but asking for tips is not cheating and often I can solve problems that I may have just abandoned in frustration if I didn't ask for help. In my original question I asked "Is the numbers just incredibly big?" I never really asked for help with the programming part I just wanted someone to point me in annother direction in case I was looking in the wrong places for mistakes.

"Hey! Debug my program for me! Don't supply any examples though! I refuse to learn from that stuff!"

Didn't really have anything to do with me not wanting to learn stuff, it has with me not wanting to spoil the moment when I get the correct answer using my own code. (even if I have gotten help from others in other forms than showing the hole solution)

I am interested in knowing what is wrong with my factorial function... Please do tell what the errors say. Does it only say "undefined behaviour"?

EDIT: I made the function by trial and error so even I have problem understanding it, should have included comments :P
Last edited on
Topic archived. No new replies allowed.