gudii9
asked on
factorial example challenge
factorial
Hi,
I am working on below challenge
http://codingbat.com/prob/p154669
Psedo code:
1. return the n multiplied by same method call with n-1
I wrote my code as below
I am failing below tests
How to improve/modify my design, code and any other alternate approaches. please advise
Hi,
I am working on below challenge
http://codingbat.com/prob/p154669
Psedo code:
1. return the n multiplied by same method call with n-1
I wrote my code as below
public int factorial(int n) {
return n * factorial(n-1);
}
I am failing below tests
Expected Run
factorial(1) → 1 Exception:java.lang.StackOverflowErr or (line number:2) X
factorial(2) → 2 Exception:java.lang.StackOverflowErr or (line number:2) X
factorial(3) → 6 Exception:java.lang.StackOverflowErr or (line number:2) X
factorial(4) → 24 Exception:java.lang.StackOverflowErr or (line number:2) X
factorial(5) → 120 Exception:java.lang.StackOverflowErr or (line number:2) X
factorial(6) → 720 Exception:java.lang.StackOverflowErr or (line number:2) X
factorial(7) → 5040 Exception:java.lang.StackOverflowErr or (line number:2) X
factorial(8) → 40320 Exception:java.lang.StackOverflowErr or (line number:2) X
factorial(12) → 479001600 Exception:java.lang.StackOverflowErr or (line number:2) X
other tests
X
How to improve/modify my design, code and any other alternate approaches. please advise
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ASKER
any improvements or alternate approaches?
Hi gudii9,
> "what is base case. why we need it?
The answer to those questions are in my post above. Did you read it?
> "why does functions calls itself forever without base case?
Because there will be nothing to stop it from looping if your function simply calls itself regardless of the argument passed to it.
Well, if you consider making it shorter to be an improvement, you could do something like this:
return n == 1 ? 1 : factorial(n-1);
but many people will find that less readable.
Are you familiar with the above short-hand alternative to the "if...else..." syntax?
> "what is base case. why we need it?
The answer to those questions are in my post above. Did you read it?
> "why does functions calls itself forever without base case?
Because there will be nothing to stop it from looping if your function simply calls itself regardless of the argument passed to it.
Well, if you consider making it shorter to be an improvement, you could do something like this:
return n == 1 ? 1 : factorial(n-1);
but many people will find that less readable.
Are you familiar with the above short-hand alternative to the "if...else..." syntax?
what is base case. why we need it?I answered that in the same sentence as I introduced the term.
why does functions calls itself forever without base case?Because that's what happens when a recursive function has no base case, by definition!
ASKER
return n == 1 ? 1 : factorial(n-1);yes. I am. I like above approach too
but many people will find that less readable.
Are you familiar with the above short-hand alternative to the "if...else..." syntax?
ASKER
Open in new window
like above?
Above passed all the tests.
what is base case. why we need it?
why does functions calls itself forever without base case?
please advise