Factorial of each element in Fibonacci series

Given the upper limit limit, print factorials of all Fibonacci Numbers smaller than limit.

Examples :

Input : limit = 20
Output : 1 1 1 2 6 120 40320 6227020800
Explanation : 
Fibonacci series in this range is 0, 1, 1, 2, 
3, 5, 8, 13. Factorials of these numbers are 
output.

Input : 50
Output : 1 1 1 2 6 120 40320 6227020800
         51090942171709440000 
         295232799039604140847618609643520000000



We know simple factorial computations cause overflow very soon. Therefore we use factorials of large numbers.

One simple solution is to generate all Fibonacci numbers one by one and compute factorial of every generated number using method discussed in factorials of large numbers

An efficient solution is based on the fact that Fibonacci numbers are increasing order. So we use previously generated factorial to compute next factorial.

C++

// CPP program to find factorial of each element
// of Fibonacci series
#include <iostream>
using namespace std;
  
// Maximum number of digits in output
#define MAX 500
  
// Finds and print factorial of n using
// factorial of prev (stored in prevFact[
// 0...size-1]
void factorial(int prevFact[], int &size,
                         int prev, int n);
  
// Prints factorials of all fibonacci
// numbers smaller than given limit.
void printfibFactorials(int limit)
{
   if (limit < 1)
      return;
  
   // Initialize first three Fibonacci
   // numbers and print factorials of
   // first two numbers.
   int a = 1, b = 1, c = 2;
   cout << a << " " << b << " ";
  
   // prevFact[] stores factorial of
   // previous fibonacci number
   int prevFact[MAX];
   prevFact[0] = 1;
  
   // Size is current size of prevFact[]
   int size = 1;
  
   // Standard Fibonacci number loop
   while (c < limit)
   {
       factorial(prevFact, size, b, c);
       a = b;
       b = c;
       c = a + b;
   }
}
  
// Please refer below article for details of
// below two functions.
  
// Function used to find factorial
int multiply(int x, int prevFact[], int size)
{
    int carry = 0;
    for (int i = 0; i < size; i++) {
        int prod = prevFact[i] * x + carry;
        prevFact[i] = prod % 10;
        carry = prod / 10;
    }
  
    // Put carry in res and increase
    // result size
    while (carry) {
        prevFact[size] = carry % 10;
        carry = carry / 10;
        size++;
    }
    return size;
}
  
// Finds factorial of n using factorial
// "prev" stored in prevFact[]. size is
// size of prevFact[]
void factorial(int prevFact[], int &size,
                         int prev, int n)
{
   for (int x = prev+1; x <= n; x++)
       size = multiply(x, prevFact, size);
  
    for (int i = size - 1; i >= 0; i--)
        cout << prevFact[i];
    cout << " ";
}
  
// Driver function
int main()
{
    int limit = 20;
    printfibFactorials(limit);
    return 0;
}

Java

// Java program to find 
// factorial of each element
// of Fibonacci series
import java.io.*;
  
class GFG
{     
    // Maximum number of
    // digits in output
    static int MAX = 500;
    static int size = 1;
      
    // Finds and print factorial 
    // of n using factorial of 
    // prev (stored in prevFact[
    // 0...size-1]
    // Finds factorial of n 
    // using factorial "prev" 
    // stored in prevFact[]. size
    // is size of prevFact[]
    static void factorial(int []prevFact, 
                          int prev, int n)
    {
    for (int x = prev + 1
             x <= n; x++)
        size = multiply(x, prevFact, size);
      
        for (int i = size - 1;
                 i >= 0; i--)
            System.out.print(prevFact[i]);
        System.out.print(" ");
    }
      
    // Prints factorials of all 
    // fibonacci numbers smaller 
    // than given limit.
    static void printfibFactorials(int limit)
    {
        if (limit < 1)
            return;
      
        // Initialize first three
        // Fibonacci numbers and
        // print factorials of
        // first two numbers.
        int a = 1, b = 1, c = 2;
        System.out.print(a + " " +
                         b + " ");
          
        // prevFact[] stores factorial 
        // of previous fibonacci number
        int []prevFact = new int[MAX];
        prevFact[0] = 1;
          
        // Standard Fibonacci
        // number loop
        while (c < limit)
        {
            factorial(prevFact, b, c);
            a = b;
            b = c;
            c = a + b;
        }
    }
      
    // Please refer below
    // article for details of
    // below two functions.
      
    // Function used to 
    // find factorial
    static int multiply(int x,
                        int []prevFact,
                        int size)
    {
        int carry = 0;
        for (int i = 0; i < size; i++) 
        {
            int prod = prevFact[i] * 
                        x + carry;
            prevFact[i] = prod % 10;
            carry = prod / 10;
        }
      
        // Put carry in
        // res and increase
        // result size
        while (carry != 0
        {
            prevFact[size] = carry % 10;
            carry = carry / 10;
            size++;
        }
        return size;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        int limit = 20;
        printfibFactorials(limit);
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)

C#

// C# program to find 
// factorial of each element
// of Fibonacci series
using System;
  
class GFG
{     
    // Maximum number of
    // digits in output
    static int MAX = 500;
      
    // Finds and print factorial 
    // of n using factorial of 
    // prev (stored in prevFact[
    // 0...size-1]
    // Finds factorial of n 
    // using factorial "prev" 
    // stored in prevFact[]. size
    // is size of prevFact[]
    static void factorial(int []prevFact, 
                          ref int size,
                          int prev, int n)
    {
    for (int x = prev + 1; x <= n; x++)
        size = multiply(x, prevFact, size);
      
        for (int i = size - 1; i >= 0; i--)
            Console.Write(prevFact[i]);
        Console.Write(" ");
    }
      
    // Prints factorials of all fibonacci
    // numbers smaller than given limit.
    static void printfibFactorials(int limit)
    {
    if (limit < 1)
        return;
      
    // Initialize first three Fibonacci
    // numbers and print factorials of
    // first two numbers.
    int a = 1, b = 1, c = 2;
    Console.Write(a + " " + b + " ");
      
    // prevFact[] stores factorial of
    // previous fibonacci number
    int []prevFact = new int[MAX];
    prevFact[0] = 1;
      
    // Size is current size
    // of prevFact[]
    int size = 1;
      
    // Standard Fibonacci
    // number loop
    while (c < limit)
    {
        factorial(prevFact, ref size, b, c);
        a = b;
        b = c;
        c = a + b;
    }
    }
      
    // Please refer below
    // article for details of
    // below two functions.
      
    // Function used to find factorial
    static int multiply(int x,
                        int []prevFact, int size)
    {
        int carry = 0;
        for (int i = 0; i < size; i++) 
        {
            int prod = prevFact[i] * 
                          x + carry;
            prevFact[i] = prod % 10;
            carry = prod / 10;
        }
      
        // Put carry in
        // res and increase
        // result size
        while (carry != 0) 
        {
            prevFact[size] = carry % 10;
            carry = carry / 10;
            size++;
        }
        return size;
    }
      
    // Driver Code
    static void Main()
    {
        int limit = 20;
        printfibFactorials(limit);
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)

PHP

<?php
// PHP program to find 
// factorial of each element
// of Fibonacci series
  
// Maximum number of
// digits in output
$MAX = 500;
$size = 1;
$prevFact = $prevFact
            array_fill(0, $MAX, 0);
  
// Finds and print factorial 
// of n using factorial of 
// prev (stored in prevFact[
// 0...size-1]
// Finds factorial of n 
// using factorial "prev" 
// stored in prevFact[]. size
// is size of prevFact[]
function factorial($prev, $n)
    {
    global $size, $prevFact;
    for ($x = $prev + 1; 
         $x <= $n; $x++)
        $size = multiply($x, $size);
      
        for ($i = $size - 1;
             $i >= 0; $i--)
            echo $prevFact[$i];
        echo " ";
    }
      
// Prints factorials of all 
// fibonacci numbers smaller 
// than given limit.
function printfibFactorials($limit)
    {
        global $MAX, $prevFact;
        if ($limit < 1)
            return;
      
        // Initialize first three
        // Fibonacci numbers and
        // print factorials of
        // first two numbers.
        $a = 1;
        $b = 1;
        $c = 2;
        echo $a . " " . $b . " ";
          
        // prevFact[] stores factorial 
        // of previous fibonacci number
        $prevFact[0] = 1;
          
        // Standard Fibonacci
        // number loop
        while ($c < $limit)
        {
            factorial($b, $c);
            $a = $b;
            $b = $c;
            $c = $a + $b;
        }
    }
      
// Function used to 
// find factorial
function multiply($x,$size)
    {
        global $prevFact;
        $carry = 0;
        for ($i = 0; 
             $i < $size; $i++) 
        {
            $prod = $prevFact[$i] * 
                    $x + $carry;
            $prevFact[$i] = $prod % 10;
            $carry = (int)($prod / 10);
        }
      
        // Put carry in
        // res and increase
        // result size
        while ($carry != 0) 
        {
            $prevFact[$size] = $carry % 10;
            $carry = (int)($carry / 10);
            $size++;
        }
        return $size;
    }
      
// Driver Code
$limit = 20;
printfibFactorials($limit);
  
// This code is contributed 
// by mits
?>


Output :

1 1 2 6 120 40320 6227020800


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Improved By : manishshaw1, Mithun Kumar




Recommended Posts:



4 Average Difficulty : 4/5.0
Based on 1 vote(s)






User Actions