Factorial of each element in Fibonacci series


In this article, we will discuss a simple program which calculates the factorial of all the numbers present in the Fibonacci series smaller than a given nums.

Problem Statement

We are given a number and our task is to generate the factorial of all the numbers present in the Fibonacci series and smaller than the given number.

Let us first understand the problem statement and requirements from the code solution with the help of examples.

Input

nums = 13

Output

Fibonacci series up to 13 is 0, 1, 1, 2, 3, 5, 

So, the factorial of all the numbers in Fibonacci series which are smaller than 13 are 1 1 2 6 120 40320

Now let us discuss the solution for this problem.

Here, we have two major problems −

  • To generate the Fibonacci series for which, we have to calculate the factorial of each number. It will be very time taking if we consider calculating factorial of each Fibonacci numbers one by one.

  • To calculate the factorial of numbers which results in very large values that cannot be stored as integers.

Approach

Coming to the solution of first major problem, we can minimize the number of calculations for the factorial of each Fibonacci number by storing the factorial of last Fibonacci number used. As we know, the Fibonacci numbers are increasing in order, we can easily store the factorial of previous Fibonacci number and calculate the factorial of next Fibonacci number using the stored factorial.

For the next major problem that is to calculate the factorial of numbers whose factorials causes overflow as they are comparatively very large and cannot be stored as integers. For this, we need to use an array to store the digits of the factorial and do the calculations accordingly.

Example

Following is the implementation of the above approach in different programming languages: C, and C++ −

C C++
#include <stdio.h>
#include <stdlib.h>

#define M 500
void func(int lastfact[], int *len, int previous, int n);
int multiply(int n, int lastfact[], int len);
void printfibFactorials(int nums) {
   if (nums < 1)
      return;
   int p = 1, q = 1, r = 2;
   printf("%d\n%d\n%d\n", p, q, r);
   int lastfact[M];
   lastfact[0] = 1;
   int len = 1;
   while (r < nums) {
      func(lastfact, &len, q, r);
      p = q;
      q = r;
      r = p + q;
   }
}
int multiply(int n, int lastfact[], int len) {
   int c = 0;
   for (int iterator = 0; iterator < len; iterator++) {
      int prod = lastfact[iterator] * n + c;
      lastfact[iterator] = prod % 10;
      c = prod / 10;
   }
   while (c) {
      lastfact[len] = c % 10;
      c = c / 10;
      len++;
   }
   return len;
}

void func(int lastfact[], int *len, int previous, int n) {
   for (int p = previous + 1; p <= n; p++)
      *len = multiply(p, lastfact, *len);
   for (int iterator = *len - 1; iterator >= 0; iterator--)
      printf("%d", lastfact[iterator]);
   printf("\n");
}

int main() {
   int nums = 13;
   printf("The factorial of numbers up to %d is\n", nums);
   printfibFactorials(nums);
   return 0;
}

Output

The factorial of numbers up to 13 is
1
1
2
2
6
120
40320
#include <iostream>
#include<bits/stdc++.h>
using namespace std;

#define M 500
void func(int lastfact[], int &len, int previous , int n);
void printfibFactorials(int nums) {
   if (nums < 1)
   return;  
   int p = 1, q = 1, r = 2;
   cout <<  p << endl << q << endl;
   int lastfact[M];
   lastfact[0] = 1;    
   int len = 1;    
   while (r < nums) {
      func( lastfact, len, q , r );
      p = q ;
      q = r ;
      r = p + q ;
   }
}
int multiply(int n, int lastfact[], int len) {
   int c = 0;
   for (int iterator = 0; iterator < len; iterator++) {
      int prod = lastfact[iterator] * n + c;
      lastfact[iterator] = prod % 10;
      c = prod / 10;
   }  
   while (c){
      lastfact[len] = c % 10;
      c = c / 10;
      len++;
   }    
   return len;
}
void func(int lastfact[], int &len, int previous, int n) {
   for (int p = previous + 1; p <= n; p++)
   len = multiply( p , lastfact , len );        
   for (int iterator = len - 1; iterator >= 0; iterator--)
   cout << lastfact[iterator];
   cout << endl;
}
int main() {
   int nums = 13;
   cout<< "The factorial of numbers upto " << nums << " is ";
   printfibFactorials(nums);
   return 0;
}

Output

The factorial of numbers upto 13 is 
1
1
2
6
120
40320

Conclusion

In this article, we calculated the factorial of all the numbers present in the Fibonacci series less than a given number that too in the best possible space complexity. To minimize the space occupied, we saved the previously calculated factorial to generate the factorial of next Fibonacci number. Also to avoid the overflow caused due to very large values of factorial numbers, we used the array to save the different digits of the factorial at different places.

Updated on: 09-Feb-2024

165 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements