“factorielle d'un grand nombre” Réponses codées

factorielle d'un grand nombre

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;
    }
};
Viper

factorielle d'un grand nombre

        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;
        }
PrashantUnity

Réponses similaires à “factorielle d'un grand nombre”

Questions similaires à “factorielle d'un grand nombre”

Plus de réponses similaires à “factorielle d'un grand nombre” dans C#

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code