Factorials of large numbers | Practice | GeeksforGeeks

Given an integer N, find its factorial.

Example 1:
Input: N = 5
Output: 120
Explanation : 5! = 1*2*3*4*5 = 120

Example 2:
Input: N = 10
Output: 3628800
Explanation :
10! = 1*2*3*4*5*6*7*8*9*10 = 3628800

Your Task:
You don't need to read input or print anything. Complete the function factorial() that takes integer N as input parameter and returns a list of integers denoting the digits that make up the factorial of N.

Expected Time Complexity : O(N2)
Expected Auxilliary Space : O(1)

Constraints:
1 ≤ N ≤ 1000
0
hansrajsaran791 day ago
Simple Python solution in less Time Complexity
       fact = 1
       for i in range(2,N+1):
           fact = fact*i
       return str(fact) 
+2
vamsiipl1 day ago
class Solution {public:   vector<int> factorial(int N)   {       vector<int> v;       for(int i = 1; i <= N; i++)       {           if(v.size() == 0)           v.push_back(i);                      int carry = 0;           for(int j = 0; j < v.size(); j++)           {               int m = v[j] * i;               m += carry;               v[j] = m % 10;               carry = m / 10;           }                      while(carry)           {               int r = carry % 10;               v.push_back(r);               carry /= 10;           }       }       reverse(v.begin(), v.end());       return v;   }};
-14
mahiratnaparkhe40522 weeks ago
Easy Peasy !
fact=1       while N!=0:           fact=fact*N           N-=1       return [fact]
+1
janvijindal052 weeks ago
 vector<int> ans;       ans.push_back(1);       for(int x=2;x<=N;x++){           int carry=0;       for(int i=0;i<ans.size();i++){           int value=ans[i]*x + carry;           ans[i]=value%10;           carry=value/10;                  }         while(carry!=0){             ans.push_back(carry%10);             carry/=10;         }       }        reverse(ans.begin(),ans.end());        return ans;
+1
pratiksehjpal20002 weeks ago
class Solution {
    static ArrayList<Integer> factorial(int N){
        int size=0,carry=0;
        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(0,1);
        size = 1;
        int val=2;
        while(val<=N)
        {
            for(int i=list.size()-1;i>=0;i--)
            {
                int temp = list.get(i)*val+carry;
                list.set(i,temp%10);
                carry = temp/10;
            }
            while(carry!=0)
            {
                list.add(0,carry%10);
                carry=carry/10;
                // size++;
            }
            val++;
        }
         return list;
    }
}
+1
dabhideep443 weeks ago
JAVA SOLUTION(0.93 SECONDS)
static ArrayList<Integer> factorial (int N) {       // Declare an arrayList       ArrayList<Integer> list = new ArrayList<>();       int size = 0, c = 0;       // adding 1 at 0th index       list.add(0, 1);       // Updating size       size = 1;       // Declare a variable to travers numbers from 2 to n       int val = 2 ;       while (val <= N) {           // Traverse arrayList from right to left           for(int i = size - 1; i >= 0; i--) {               // Updating value in arrayList               int temp = list.get(i) * val + c;               // Store last digit at index and remaining to carry               list.set(i, temp % 10);               // Update carry               c = temp / 10;           }           // Insert carry digit by digit to the beginning of the ArrayList           while (c != 0) {               list.add(0, c % 10);               c = c / 10;               size++;           }           val++;       }       return list;   }
0
jotarokujo3 weeks ago
static ArrayList<Integer> factorial(int n){
       java.math.BigInteger[] fact
       	= new java.math.BigInteger[n + 1];
       fact[1] = java.math.BigInteger.valueOf(1);
       for (int i = 2; i <= n; i += 1) {
           fact[i] = fact[i - 1]
           	.multiply(java.math.BigInteger.valueOf(i));
       }
       char[] ans = fact[n].toString().toCharArray();
       ArrayList<Integer> sol = new ArrayList<>();
       for (int i = 0; i < ans.length; i += 1) {
           sol.add(ans[i] - '0');
       }
       return sol;
   }
+1
bhavinraichura283 weeks ago
vector<int> factorial(int n){   // code here   vector<int>digits;   int val;
   for (int i =1;i<=n;i++){       int j =0;       int carry = 0;       if (i==1){           digits.push_back(1);           continue;       }       while(j<digits.size()){           val = digits[j]*i + carry;           digits[j]= val%10;           carry = val/10;           j++;       }
       while(carry!=0){           digits.push_back(carry%10);           carry=carry/10;       }   }   reverse(digits.begin(),digits.end());   return digits;   }
-2
amiransarimy1 month ago
Simple Python Solutions
 
def factorial(self, N):
       res = self.factorialHelper(N)
       return list(str(res))
        
  def factorialHelper(self,num):
        if num == 0 or num == 1:
            return 1  
        else:
            return num*self.factorialHelper(num-1) 
+1
kaustubhdwivedi17291 month ago
The number of digits in 1000 factorial is 2568.
    vector<int> factorial(int N){
    	int arr[2568];
        int n = 2568;
        arr[n-1] = 1;
        int res = 1;
        int carry = 0;
        
        for(int i = 2; i <= N; i++){
            carry = 0;
            for(int j = n-1; j > n-1-res; j--){
                int temp = arr[j]*i+carry;
                arr[j] = temp%10;
                carry = temp/10;
            }
            while(carry){
                arr[n-1-res] = carry%10;
                carry /= 10;
                res++;
            }
        }
    
        vector<int> ans(res, 0);
        int j = 0;
        for(int i = n-res; i < n; i++)
            ans[j++] = arr[i];
        return ans;
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.