MATLAB - Factorial



A factorial of a non-negative integer 'n,' denoted as 'n!', is defined as the product of all positive integers from 1 to 'n.' So mathematically the syntax is −

n! = 1 × 2 × 3 × ... × n

So for example factorial of 5 is going to be

5! = 1 × 2 × 3 × 4 × 5  =  120

Syntax

result = factorial(n)
  • Here n is the non-negative integer for which you want to calculate the factorial.
  • Result will store the calculated factorial value.

In mathematical notation, n factorial, denoted as n!, is frequently represented with an exclamation point. It's important to note that in MATLAB, using n! as a syntax for computing the factorial of n is not valid.

Examples on Calculating Factorials

Let us see a few examples on calculating factorials of a given number.

Example 1: Calculate factorial of number n

To calculate 5!, you can use the factorial() function in MATLAB −

result = factorial(5)

When you execute in matlab command window the output is −

>> result = factorial(5)

result = 120

The above code will give you the result: result = 120, because 5! is equal to 120.

Example 2: Factorial of a Large Number

MATLAB can handle factorials of even large numbers.

n = 20;
result = factorial(n)

When you execute the same in matlab command window the output is −

>> n = 20;
result = factorial(n)

result =  2.4329e+18

Example 3: Calculating Factorials of an Array

Suppose you have an array A with several values for which you want to calculate the factorials −

A = [3, 4, 5, 6, 7];
result = factorial(A)

The output is −

result = 

    6  24  120  720  5040

Example 4: Calculating Factorials of an Array

Consider the array shown below , for which we are going to calculate the factorial.

num_array = [0 1 2; 3 4 5];
result = factorial(num_array)

On execution the output is −

result =

   1     1     2
   6    24   120

Example 5: Factorial of Unsigned Integers

uints = uint64([7 10 15 20]);
result = factorial(uints)

So we have vector of unsigned ints as [7 10 15 20]

On execution the output is −

result = 

   5.0400e+03   3.6288e+06   1.3077e+12   2.4329e+18

Factorial Calculation Using Functions in Matlab

You can create a MATLAB function to calculate the factorial of a number. Here's a simple function to do that −

function fact = factorial_custom(n)
   if n < 0
      error('Factorial is undefined for negative numbers.');
   elseif n == 0 || n == 1
      fact = 1;
   else
      fact = 1;
      for i = 2:n
         fact = fact * i;
      end
   end
end

This factorial_custom function takes an input n, and it calculates the factorial of n using a for loop. It handles negative numbers and returns an error message for them. For 0 and 1, the factorial is defined as 1. For other positive integers, it calculates the factorial using a loop.

You can execute in matlab as follows −

factorial custom

Calculate Factorial Using For Loop

You can calculate a factorial using a for loop as follows −

n=6;
result = 1;
for i = 1:n
   result = result * i;
end

Here, result is initialized to 1, and the loop multiplies it by the numbers from 1 to n.

When you execute the same in matlab command window −

>> n = 6;
result = 1;
for i = 1:n
   result = result * i;
end
>> result

result =

   720

Calculate Factorial Using While Loop

A factorial can also be calculated with a while loop −

n = 6;
result = 1;
i = 1;
while i <= n
   result = result * i;
   i = i + 1;
end

This code is similar to the for loop method but uses a while loop instead.

The execution in matlab command window is as follows −

>> n = 6;
   result = 1;
   i = 1;
   while i <= n
      result = result * i;
      i = i + 1;
   end
>> result

result =

   720
Advertisements