Search
 
SCRIPT & CODE EXAMPLE
 

CPP

factorial of large number

class Solution {
public:
    #define pb push_back
    void multiply(vector<int>& arr, int no){
        int carry = 0;
        
        // Multiply each digit with the correspomding no, like in simple maths of mult.
        for(int i=0;i<arr.size();i++){
            int prod = arr[i] * no + carry;
            arr[i] = prod % 10;
            carry = prod / 10;
        }
        
        // If carry is sill there
        while(carry){
            arr.pb(carry % 10);
            carry = carry / 10;
        }
    }
    
    vector<int> factorial(int n){
        // code here
        vector<int>arr = {1};
        
        // 5! = 5*4*3*2*1
        // The crux here is how to multiply two numbers if it is large
        for(int i=2;i<=n;i++){
            multiply(arr, i);
        }
        
        // print the array or return the array
        reverse(arr.begin(), arr.end());
        
        return arr;
    }
};
Comment

factorial of large number

        public static void Factorial(int n)
        {
            var number = new int[999999];
            number[0] = 1;
            int numLength = 1;
            for (int times = 2; times <= n; times++)
                numLength = Multiply(times, number,numLength);

            for (int i = numLength - 1; i >= 0; i--)
                Console.Write(number[i]);
        }

        
        static int Multiply(int times, int[] number,int numLength)
        {
            int carry = 0; 
            for (int i = 0; i < numLength; i++)
            {
                int product = number[i] * times + carry;
                number[i] = product % 10;
                carry = product / 10;
            }

            while (carry != 0)
            {
                number[numLength] = carry % 10;
                carry /= 10;
                numLength++;
            }
            return numLength;
        }
Comment

PREVIOUS NEXT
Code Example
Cpp :: map in c 
Cpp :: c++ check if debug or release visual studio 
Cpp :: how to create a file in c++ 
Cpp :: cpp template 
Cpp :: hashmap c++ 
Cpp :: access last element of set c++ 
Cpp :: inheritance in c++ 
Cpp :: how to add space in c++ 
Cpp :: number of nodes of bst cpp 
Cpp :: passing custom function in sort cpp 
Cpp :: resize vector c++ 
Cpp :: take a function as an argument in c++ 
Cpp :: c++ check if key exists in map 
Cpp :: c++ std string to float 
Cpp :: C++ wchar_t 
Cpp :: loop execution descending order in c++ 
Cpp :: cmake g++ address sanitizer 
Cpp :: c++ polymorphism 
Cpp :: what is the meaning of life and everything in the universe 
Cpp :: nullptr c++ 
Cpp :: c++ online compiler 
Cpp :: error uploading arduino code 
Cpp :: int to string C++ Using stringstream class 
Cpp :: what algorithm does bitcoin use 
Cpp :: convert single character string to char c++ 
Cpp :: abs in c++ used for 
Cpp :: print all subsequences 
Cpp :: c++ stl 
Cpp :: c++ method name 
Cpp :: vector insert to end 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =