ARTICLE

How to find the "Factorial of an Number" an Algorithm

Posted by Sapna Articles | Algorithms and VB.NET February 11, 2011
Factorials are very simple, the factorial of any number is that number times the factorial of 1 smaller than that number.
 
Reader Level:


Factorials are very simple things. They're just products, indicated by an exclamation mark. For instance, "four factorial" is written as "4!" and means 1x2x3x4 = 24. the factorial of any number is that number times the factorial of (1 smaller than that number), factorial(N), for scalar N, is the product of all the integers from 1 to N, i.e. prod(1:n). When N is an N-dimensional array, factorial(N) is the factorial for each element of N.
The factorial function can also be defined for non-integer values using more advanced mathematics. This more generalized definition is used by advanced calculators and mathematical software such as Maple or Mathematica.

factorial1.gif

factorial2.gif

Finding Factorial of a given number is another interesting problem. Mathematically represented as n.
For ex:

5! = 5*4*3*2*1.

Not to forget that 1! = 1 and 0! = 1.

We can now generalize the factorial of a given number which is any thing other than zero and one as the product of all the numbers ranging from given number to 1.

i.e n! = n * (n - 1) * (n - 2 ) * ...*1

Algorithm : Factorial

Input : n

Output : Factorial of n

Method
    fact = 1
    for i = n to 1 in steps of -1 do
        fact = fact*i
    end_for
    display 'factorial = ',fact

Algorithm ends

In the above algorithm we have implemented the logic of the equation

n! = n * (n - 1) * (n - 2 ) * ... *1.

The same can be achieved by the following algorithm which follows incremental steps rather than decremental steps of the given algorithm.

Algorithm : Factorial

Input : n

Output : Factorial of n

Method
    fact = 1
    for i = 1 to n in steps of 1 do
        fact = fact*i
    end_for
    display 'factorial = ',fact

Algorithm ends
  

Login to add your contents and source code to this article
share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor