Recursive Program to find Factorial of a large number
Last Updated :
09 Jan, 2023
Given a large number N, the task is to find the factorial of N using recursion.
Factorial of a non-negative integer is the multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720.
Examples:
Input : N = 100
Output : 933262154439441526816992388562667004-907159682643816214685929638952175999-932299156089414639761565182862536979-208272237582511852109168640000000000-00000000000000
Input : N = 50
Output : 3041409320171337804361260816606476884-4377641568960512000000000000
Iterative Approach: The iterative approach is discussed in Set 1 of this article. Here, we have discussed the recursive approach.
Recursive Approach: To solve this problem recursively, the algorithm changes in the way that calls the same function recursively and multiplies the result by the number n. Follow the steps below to solve the problem:
- If n is less than equal to 2, then multiply n by 1 and store the result in a vector.
- Otherwise, call the function multiply(n, factorialRecursiveAlgorithm(n – 1)) to find the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > multiply( long int n, vector< int > digits)
{
long int carry = 0;
for ( long int i = 0; i < digits.size(); i++) {
long int result
= digits[i] * n + carry;
digits[i] = result % 10;
carry = result / 10;
}
while (carry) {
digits.push_back(carry % 10);
carry = carry / 10;
}
return digits;
}
vector< int > factorialRecursiveAlgorithm(
long int n)
{
if (n <= 2) {
return multiply(n, { 1 });
}
return multiply(
n, factorialRecursiveAlgorithm(n - 1));
}
int main()
{
long int n = 50;
vector< int > result
= factorialRecursiveAlgorithm(n);
for ( long int i = result.size() - 1; i >= 0; i--) {
cout << result[i];
}
cout << "\n" ;
return 0;
}
|
Java
import java.util.*;
class GFG{
static Integer []multiply( int n, Integer []digits)
{
int carry = 0 ;
for ( int i = 0 ; i < digits.length; i++) {
int result
= digits[i] * n + carry;
digits[i] = result % 10 ;
carry = result / 10 ;
}
LinkedList<Integer> v = new LinkedList<Integer>();
v.addAll(Arrays.asList(digits));
while (carry> 0 ) {
v.add( new Integer(carry % 10 ));
carry = carry / 10 ;
}
return v.stream().toArray(Integer[] :: new );
}
static Integer []factorialRecursiveAlgorithm(
int n)
{
if (n <= 2 ) {
return multiply(n, new Integer[]{ 1 });
}
return multiply(
n, factorialRecursiveAlgorithm(n - 1 ));
}
public static void main(String[] args)
{
int n = 50 ;
Integer []result
= factorialRecursiveAlgorithm(n);
for ( int i = result.length - 1 ; i >= 0 ; i--) {
System.out.print(result[i]);
}
System.out.print( "\n" );
}
}
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
static int [] multiply( int n, int [] digits)
{
int carry = 0;
for ( int i = 0; i < digits.Length; i++)
{
int result
= digits[i] * n + carry;
digits[i] = result % 10;
carry = result / 10;
}
LinkedList< int > v = new LinkedList< int >();
foreach ( int i in digits)
{
v.AddLast(i);
}
while (carry > 0)
{
v.AddLast(( int )(carry % 10));
carry = carry / 10;
}
return v.ToArray();
}
static int [] factorialRecursiveAlgorithm(
int n)
{
if (n <= 2)
{
return multiply(n, new int [] { 1 });
}
return multiply(
n, factorialRecursiveAlgorithm(n - 1));
}
public static void Main()
{
int n = 50;
int [] result = factorialRecursiveAlgorithm(n);
for ( int i = result.Length - 1; i >= 0; i--)
{
Console.Write(result[i]);
}
Console.Write( "\n" );
}
}
|
Python3
def multiply(n, digits):
carry = 0
for i in range ( len (digits)):
result = digits[i] * n + carry
digits[i] = result % 10
carry = result / / 10
while (carry):
digits.append(carry % 10 )
carry = carry / / 10
return digits
def factorialRecursiveAlgorithm(n):
if (n < = 2 ):
return multiply(n, [ 1 ])
return multiply(
n, factorialRecursiveAlgorithm(n - 1 ))
if __name__ = = "__main__" :
n = 50
result = factorialRecursiveAlgorithm(n)
for i in range ( len (result) - 1 , - 1 , - 1 ):
print (result[i], end = "")
|
Javascript
<script>
function multiply(n, digits)
{
var carry = 0;
for ( var i = 0; i < digits.length; i++) {
var result
= digits[i] * n + carry;
digits[i] = result % 10;
carry = parseInt(result / 10);
}
while (carry>0) {
digits.push(carry % 10);
carry = parseInt(carry / 10);
}
return digits;
}
function factorialRecursiveAlgorithm(
n)
{
if (n <= 2) {
return multiply(n, [ 1 ]);
}
return multiply(
n, factorialRecursiveAlgorithm(n - 1));
}
var n = 50;
var result = factorialRecursiveAlgorithm(n);
for ( var i = result.length - 1; i >= 0; i--) {
document.write(result[i]);
}
document.write( "<br>" );
</script>
|
Output
30414093201713378043612608166064768844377641568960512000000000000
Time Complexity: O(n*log(n))
Auxiliary Space: O(K), where K is the maximum number of digits in the output
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...