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
vibhorjaiswal992 days ago
class Solution {
    
    
    void findans(vector<int>& vec, int n)
    {
        long long unsigned int carry = 0;
        
        for(int i=0; i<vec.size(); i++)
        {
            long long unsigned int mul = (vec[i] * n) + carry;
            
            vec[i] = mul%10;
            
            carry = mul/10;
        }
        
        if(carry != 0)
        {
            while(carry > 0)
            {
                vec.push_back(carry%10);
                carry /= 10;
            }
        }
    }
    
public:
    vector<int> factorial(int n){
        // code here
        vector<int> ans;
        
        ans.push_back(1);
        
        for(int i=2; i<=n; i++)
        {
            findans(ans, i);
        }
        
        if(ans[ans.size()-1] == 0)
            ans.pop_back();
            
        reverse(ans.begin(), ans.end());
        
        return ans;
        
    }
};
0
rahulchittora2 days ago
class Solution:
   def factorial(self, N): 
       res = [1] #starting with 1
       for i in range(2, N + 1): #from 2 to N+1 as range excluded N
           print(res)
           carry = 0 #at start of next integer resetting carry
           p = len(res) - 1 #traversing from end to start
           while p >= 0:
               mul = res[p] * i 
               res[p] = (mul + carry)%10
               carry = (mul + carry)//10
               print(p,mul,carry, res, (mul + carry)%10)
               p -= 1
           if carry > 0:
               res.insert(0, carry)
       return res

0
apoorvposwal993 days ago
vector<int> factorial(int N){
       vector<int> rem;
       rem.push_back(1);
       for(int x=2;x<=N;x++){
           int carry=0;
           for(int i=rem.size()-1;i>=0;i--){
               int value=rem[i]*x+carry;
               rem[i]=value%10;
               carry=value/10;
           }
           while(carry!=0){
               rem.insert(rem.begin(),carry%10);
               carry=carry/10;
           }
       }
       return rem;
   }
0
majumdartanmay684 days ago
 static ArrayList<Integer> factorial(int N){
    final ArrayList<Integer> ans = new ArrayList<Integer>();
     if (N == 0 || N == 1) {
         ans.add(1);
         return ans;
     }
     
     String fact = "1";
     for (int i = 2; i <= N; i++) {
         fact = mulNum(fact, String.valueOf(i));
     }
     
     for (final char elem : fact.toCharArray()) {
         ans.add(elem - '0');
     }
     
     return ans;
   }
   
   
   static String convert(final int[] arr) {
       final StringBuilder builder = new StringBuilder();
       boolean all0 = true;
       for (final int a : arr) {
           if (a != 0) {
               all0 = false;
           }
           if (!all0) {
               builder.append(a);
           }
       }
       
       if(all0) {
           return "0";
       }
       
       return builder.toString();
       
   }
   static String mulNum(final String s1, final String s2) {
       final int n1 = s1.length();
       final int n2 = s2.length();
       final int[] result = new int[n1 + n2];
       
       for (int i = n2 - 1; i >= 0; --i) {
           final int level = n2 - i - 1;
           int index = result.length - level - 1;
           int carry = 0;
           for (int j = n1 - 1; j >=0; --j) {
               final int num1 = s1.charAt(j) - '0';
               final int num2 = s2.charAt(i) - '0';
               final int prod = num1 * num2 + carry + result[index--];
               carry = prod / 10;
               result[index + 1] = prod - carry * 10;
           }
           if (carry != 0) {
               result[index] = carry;
               carry = 0;
           }
       }
       
       return convert(result);
   }
0
abhishekvicky123455 days ago
/*Java Solution*/
class Solution {   private  int check(int k, int[] ary, int res) { int carry=0; for(int i=0;i<res;i++) { int prod=ary[i]*k+carry; ary[i]=prod%10; carry=prod/10 }   while(carry>0) {  ary[res]=carry%10;  carry=carry/10;  res++; }   return res;}    ArrayList<Integer> factorial(int x){ int[] ary=new int[10000]; ary[0]=1; int res=1; for(int i=1;i<=x;i++) {  res=check(i,ary,res); }ArrayList<Integer> hs=new ArrayList<>(); for(int i=res-1;i>=0;i--) {  hs.add(ary[i]);    }     return hs;}} 
0
kushagratomar6 days ago
C++ CODE
 
class Solution {public:   vector<int> factorial(int N){       // code here       vector<int> res;       res.push_back(1);       for(int x=2;x<=N;x++) {           int carry=0;           for (int i=0;i<res.size();i++) {               int val = res[i]*x + carry;               res[i] = val % 10;               carry = val / 10;           }           while(carry!=0) {               res.push_back(carry%10);               carry /= 10;           }       }       reverse (res.begin(),res.end());       return res;   }};
+1
2019uee01191 week ago
vector<int> factorial(int N)   {       vector<int>v;       int carry=0;       v.push_back(1);       for(int i=2;i<=N;i++)       {           carry=0;           for(int j=0;j<v.size();j++)           {               v[j]=i*v[j]+carry;               carry=v[j]/10;               v[j]=v[j]%10;               if(j+1==v.size())               {                      while(carry>=1)                   {                       v.push_back(carry%10);                       carry=carry/10;                   }                   break;               }           }       }          reverse(v.begin(),v.end());       return v;   }
0
puja_gupta1 week ago
//Python Solution
 
class Solution:   def factorial(self, N):       fact=1       for i in range(1,N+1):           fact*=i       return str(fact)
0
deeksharajawat142 weeks ago
//Python Solution
 
class Solution:   def factorial(self, N):       p=1       L=[]       for i in range(2,N+1):           p=p*i       L.append(p)       return L
0
divyalodhi0509
This comment was deleted.
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.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems 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.