Home » SQL & PL/SQL » SQL & PL/SQL » Factorial Function Confusing. (Oracle DB 10.2.0.4.0)



Re: Factorial Function Confusing. [message #677694 is a reply to message #677690] 
Mon, 07 October 2019 11:01 
Solomon Yakobson
Messages: 3081 Registered: January 2010 Location: Connecticut, USA

Senior Member 


In addition, function can be optimized a bit to reduce recursion level:
CREATE OR REPLACE function factorial (
n positive
) return positive
is
begin
if n <= 2 then
return n;
else
return N * factorial(n1);
end if;
end;
/
SY.




Re: Factorial Function Confusing. [message #677698 is a reply to message #677697] 
Tue, 08 October 2019 00:38 

Michel Cadot
Messages: 67952 Registered: March 2007 Location: Nanterre, France, http://...

Senior Member Account Moderator 


You missed this one:
return N * factorial(n1);
If N=1 then it returns 1 but if n <> 1 then it returns N multiplies by factorial(n1).
If you put the computation of your factorial(4) call you get:
4 * factorial(3) * factorial(2) * factorial(1) (and this later is 1)
[Updated on: Tue, 08 October 2019 00:41] Report message to a moderator





Goto Forum:
Current Time: Mon Sep 27 16:57:37 CDT 2021
