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!
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.
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?
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.
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.
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