Home » SQL & PL/SQL » SQL & PL/SQL » Factorial Function Confusing. (Oracle DB 10.2.0.4.0)
Factorial Function Confusing. Mon, 07 October 2019 06:12
 mamalik Messages: 268Registered: November 2008 Location: Pakistan Senior Member
Dear All,

I saw following code on internet while reading about recursive function. I copied following code and test on my environment. Please see below code.

CREATE OR REPLACE function factorial (
n positive
) return positive
is
begin
if n = 1 then
return n;
else
return N * factorial(n-1);
end if;
end;
/
Then i run following statement and got correct result.
Select Factorial(4) from dual;

Confusion is that in function it is written that "If n = 1 then return n". But when i will call function to calculate factorial of 4, then 4 value is passed and it will be decreased by 1 until its value is 1, when n will be 1 then value will be returned after calculating desired value.

So i think in each case result should be 1, so how is it returning correct value.

Best Regards,
Asif.
Re: Factorial Function Confusing. [message #677692 is a reply to message #677690] Mon, 07 October 2019 07:56
 Amine Messages: 312Registered: March 2010 Senior Member
this is the purpose of recursive functions.

Make beautiful code, readable, scalable and maintainable.

you have to run the code step by step, to see that in fact, the function returns the factorial of n.

for n=4, it returns 4*fact(3).
then fact(3) returns 3*fact(2)
then fact(2) returns 2*fact(1)
then as fact(1) = 1 then the stop condition is satisfied, and the resistivity is ended.

then when you come back, you'll have 4*3*2*1=24.
Re: Factorial Function Confusing. [message #677693 is a reply to message #677690] Mon, 07 October 2019 09:59
 Michel Cadot Messages: 66715Registered: March 2007 Location: Nanterre, France, http://... Senior MemberAccount Moderator

In execution graph this is:
factorial(4)
|
+-> 4 * factorial(3)
|
+-> 3 * factorial(2)
|
+-> 2 * factorial(1)
|
+-> 1
then pop the returned results from the last to the first one applying the operation:
((((1) * 2) * 3) * 4)
Re: Factorial Function Confusing. [message #677694 is a reply to message #677690] Mon, 07 October 2019 11:01
 Solomon Yakobson Messages: 2897Registered: 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(n-1);
end if;
end;
/

SY.
Re: Factorial Function Confusing. [message #677697 is a reply to message #677690] Mon, 07 October 2019 23:27
 mamalik Messages: 268Registered: November 2008 Location: Pakistan Senior Member

if n = 1 then
return n;

If about statement if N=1 then value of N will be returned. If N=1 then how n is return 24.

Regards,
Asif.
Re: Factorial Function Confusing. [message #677698 is a reply to message #677697] Tue, 08 October 2019 00:38
 Michel Cadot Messages: 66715Registered: March 2007 Location: Nanterre, France, http://... Senior MemberAccount Moderator

You missed this one:
return N * factorial(n-1);
If N=1 then it returns 1 but if n <> 1 then it returns N multiplies by factorial(n-1).

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

Re: Factorial Function Confusing. [message #677699 is a reply to message #677697] Tue, 08 October 2019 00:39
 _jum Messages: 559Registered: February 2008 Senior Member
Quote:
In addition, function can be optimized a bit to reduce recursion level:
Btw. would add the DETERMINISTIC clause for same reason, if one uses the function more than once.

[Updated on: Tue, 08 October 2019 00:42]

Report message to a moderator

Re: Factorial Function Confusing. [message #677710 is a reply to message #677699] Tue, 08 October 2019 09:04
 JPBoileau Messages: 79Registered: September 2017 Member
Not to mention that this function will only work for values up to 12 as it is written.

JP
 Previous Topic: Query Not giving Proper Output and How to write Query In effective way Next Topic: How to get response from xml in oracle
Goto Forum:

Current Time: Sun Dec 08 10:40:26 CST 2019