﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Stefan Popovski / Article Discussions / Article Discussions by Author  / Recursive Factorial Function ( n ! ) / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 18 Jun 2013 23:21:41 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Recursive Factorial Function ( n ! )</title><link>http://www.sqlservercentral.com/Forums/Topic406547-124-1.aspx</link><description>[quote][b]davidrudd (4/12/2013)[/b][hr]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) ASBEGINDECLARE @fact int, @query varchar(255)SET @fact = 1IF(@num = 0)BEGINSET  @fact = 1ENDELSEBEGINWHILE(@num &amp;gt;0)BEGINSET @fact = @fact * @numSET @num = @num -1ENDENDRETURN @factEND[/quote]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.[code="sql"]--===== 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 &amp;gt; 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;[/code]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.</description><pubDate>Fri, 12 Apr 2013 22:04:04 GMT</pubDate><dc:creator>Jeff Moden</dc:creator></item><item><title>RE: Recursive Factorial Function ( n ! )</title><link>http://www.sqlservercentral.com/Forums/Topic406547-124-1.aspx</link><description>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) ASBEGINDECLARE @fact int, @query varchar(255)SET @fact = 1IF(@num = 0)BEGINSET  @fact = 1ENDELSEBEGINWHILE(@num &amp;gt;0)BEGINSET @fact = @fact * @numSET @num = @num -1ENDENDRETURN @factEND</description><pubDate>Fri, 12 Apr 2013 10:01:04 GMT</pubDate><dc:creator>davidrudd</dc:creator></item><item><title>RE: Recursive Factorial Function ( n ! )</title><link>http://www.sqlservercentral.com/Forums/Topic406547-124-1.aspx</link><description>thanks for the help</description><pubDate>Tue, 31 Aug 2010 12:02:45 GMT</pubDate><dc:creator>salonimahajan934</dc:creator></item><item><title>RE: Recursive Factorial Function ( n ! )</title><link>http://www.sqlservercentral.com/Forums/Topic406547-124-1.aspx</link><description>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.</description><pubDate>Thu, 07 May 2009 11:11:23 GMT</pubDate><dc:creator>j-1064772</dc:creator></item><item><title>Recursive Factorial Function ( n ! )</title><link>http://www.sqlservercentral.com/Forums/Topic406547-124-1.aspx</link><description>Comments posted to this topic are about the item [B]&lt;A HREF="/scripts/T-SQL+Aids/30937/"&gt;Recursive Factorial Function ( n ! )&lt;/A&gt;[/B]</description><pubDate>Wed, 03 Oct 2007 18:00:10 GMT</pubDate><dc:creator>s_popovski</dc:creator></item></channel></rss>