SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Recursive Factorial Function ( n ! )


Recursive Factorial Function ( n ! )

Author
Message
s_popovski
s_popovski
SSC-Enthusiastic
SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)SSC-Enthusiastic (105 reputation)

Group: General Forum Members
Points: 105 Visits: 28
Comments posted to this topic are about the item Recursive Factorial Function ( n ! )



j-1064772
j-1064772
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1209 Visits: 1213
Abslolutely ridiculous example.

Bad advice.

This is the most inefficient way of calculating factorials.

At least if there was a warning that this is intended to show how recursion works along with a warning to novices that in the case of a factorial this definitely not the proper way of doing and why.
salonimahajan934
salonimahajan934
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 1
thanks for the help
davidrudd
davidrudd
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 11
A better solution with no limits.

Thanks to:
http://2smart4school.com/tsql-stored-procedure-to-get-factorial-of-a-given-number/


CREATE PROCEDURE Factorial (@num INT) AS
BEGIN

DECLARE @fact int, @query varchar(255)
SET @fact = 1
IF(@num = 0)
BEGIN
SET @fact = 1
END
ELSE
BEGIN
WHILE(@num >0)
BEGIN
SET @fact = @fact * @num
SET @num = @num -1
END
END
RETURN @fact
END



Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)SSC Guru (203K reputation)

Group: General Forum Members
Points: 203366 Visits: 41949
davidrudd (4/12/2013)
A better solution with no limits.

Thanks to:
http://2smart4school.com/tsql-stored-procedure-to-get-factorial-of-a-given-number/


CREATE PROCEDURE Factorial (@num INT) AS
BEGIN

DECLARE @fact int, @query varchar(255)
SET @fact = 1
IF(@num = 0)
BEGIN
SET @fact = 1
END
ELSE
BEGIN
WHILE(@num >0)
BEGIN
SET @fact = @fact * @num
SET @num = @num -1
END
END
RETURN @fact
END



Not quite true. That stored procedure is limited to a factorial of only 12 because of the INT datatype. Because it's a proc, it's difficult to use in a non-RBAR environment. And I'm not sure that I'd trust anyone's code that blatantly had an unused variable in it. ;-)

I guess I don't understand why people insist on recalculating that which will not change. For example, no matter how many times you calculate it, 170! will always return the same number. So why not calculate it just once and store it in a "helper" table?

Here's how to make a Factorial "helper" table.

--===== Create the table with columns for N and N!.
-- This will prepopulate the values of N, as well.
SELECT TOP 171
N = IDENTITY(INT,0,1),
[N!] = CAST(0 AS FLOAT)
INTO dbo.Factorial
FROM sys.all_columns
;
--===== Add the quintessential PK for max performance of future lookups
ALTER TABLE dbo.Factorial
ADD CONSTRAINT PK_Factorial
PRIMARY KEY CLUSTERED (N) WITH FILLFACTOR = 100
;
--===== Declare a variable that well need to keep track of the previous product.
DECLARE @Factorial FLOAT;

--===== Update the table with factorials.
UPDATE f
SET @Factorial = [N!] = CASE WHEN N > 0 THEN @Factorial * N ELSE 1 END
FROM dbo.Factorial f WITH (TABLOCKX, INDEX(1))
OPTION (MAXDOP 1)
;
--===== Show our work
SELECT * FROM dbo.Factorial ORDER BY N;




Then all you have to do is join to the factorial table for any number of rows in a set based fashion instead of recalculating the same thing over and over.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search