Comments on: Factorial in Oracle SQL – using both new Recursive Subquery and classic Connect By approach Friends of Oracle and Java Sat, 25 Apr 2015 11:36:47 +0000 hourly 1 By: Laurent Schneider Tue, 13 Dec 2011 14:08:13 +0000 indeed the logarythm of the product is the sum of the logarythms. 
And exp(ln(x))=x 
Then exp(ln(x1*x2*x3))=exp(ln(x1)+ln(x2)+ln(x3))=exp(sum(ln(x)))

I have not been to conferences for a while. But next time you come to Switzerland drop me a line :-)

By: Stew Ashton Tue, 13 Dec 2011 12:13:39 +0000 Laurent often comes up with neat tricks like this :)
I had to look this up, and I found a layman’s explanation in
ln(xy) = ln(x) + ln(y)
So Laurent is actually multiplying the logarithms of 1, 2, 3, etc. by adding them. Then the exponent is just the logarithm in reverse to get back the real value he wants.

By: Lucas Jellema Tue, 13 Dec 2011 11:23:04 +0000 Hi Laurent,

Thanks – that one works as well. Although I am not sure why exp(sum(ln( n))) gives the factorial. I suppose the Maths behind this is beyond me. I just was looking for a demonstration of recursive-ness…

How are you doing by the way? Will I meet you again one of these days on a conference somewhere?


By: Laurent Schneider Tue, 13 Dec 2011 11:11:47 +0000 alternatively :

SQL> select rownum,exp(sum(ln(rownum)) over (order by rownum)) from dual connect by level<10;
———- —————————————
         1                                       1
         2                                       2
         3                                       6
         4                                      24
         5                                     120
         6                                     720
         7                                    5040
         8                                   40320
         9                                  362880

By: Gerwin Hendriksen Tue, 13 Dec 2011 10:49:23 +0000 Hi Lucas, it is nice to see that you actually programmed the factorial in SQL. I use a PL/SQL function to do the same thing and saw that a lot of the examples on the internet don’t correct for the factorial of zero to be one. When I used the factorial to program the Erlang-C formula in PL/SQL I found out that the factorial is not part of standard SQL. I think it might be a nice enhancement for Oracle SQL to make the factorial standard SQL.
Regards, Gerwin