factorial of big numbers with strings in c++

11,871

Solution 1

There's a web site that will calculate factorials for you: http://www.nitrxgen.net/factorialcalc.php. It reports:

The resulting factorial of 250! is 493 digits long. The result also contains 62 trailing zeroes (which constitutes to 12.58% of the whole number)

3232856260909107732320814552024368470994843717673780666747942427112823747555111209488817915371028199450928507353189432926730931712808990822791030279071281921676527240189264733218041186261006832925365133678939089569935713530175040513178760077247933065402339006164825552248819436572586057399222641254832982204849137721776650641276858807153128978777672951913990844377478702589172973255150283241787320658188482062478582659808848825548800000000000000000000000000000000000000000000000000000000000000

Many systems using C++ double only work up to 1E+308 or thereabouts; the value of 250! is too large to store in such numbers.

Consequently, you'll need to use some sort of multi-precision arithmetic library, either of your own devising using C++ string values, or using some other widely-used multi-precision library (GNU GMP for example).

Solution 2

Not sure why you are trying to use string. Probably to save some space by not using integer vector? This is my solution by using integer vector to store factorial and print.Works well with 400 or any large number for that matter!

//Factorial of a big number

#include<iostream>
#include<vector>
using namespace std;



int main(){
    int num;
    cout<<"Enter the number :";
    cin>>num;
    vector<int> res;
    res.push_back(1);
    int carry=0;
    for(int i=2;i<=num;i++){
        for(int j=0;j<res.size();j++){
            int tmp=res[j]*i;
            res[j]=(tmp+carry)%10 ;
            carry=(tmp+carry)/10;

        }
        while(carry!=0){
            res.push_back(carry%10);
            carry=carry/10;
        }

    }

    for(int i=res.size()-1;i>=0;i--) cout<<res[i];
    cout<<endl;





    return 0;
}

Enter the number :400 Factorial of 400 :64034522846623895262347970319503005850702583026002959458684445942802397169186831436278478647463264676294350575035856810848298162883517435228961988646802997937341654150838162426461942352307046244325015114448670890662773914918117331955996440709549671345290477020322434911210797593280795101545372667251627877890009349763765710326350331533965349868386831339352024373788157786791506311858702618270169819740062983025308591298346162272304558339520759611505302236086810433297255194852674432232438669948422404232599805551610635942376961399231917134063858996537970147827206606320217379472010321356624613809077942304597360699567595836096158715129913822286578579549361617654480453222007825818400848436415591229454275384803558374518022675900061399560145595206127211192918105032491008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Solution 3

The code below uses unsigned double long to calculate very large digits.

#include<iostream.h>


int main()
{
    long k=1;
    while(k!=0)
    {
        cout<<"\nLarge Factorial Calculator\n\n";
        cout<<"Enter a number be calculated:";

        cin>>k;

        if (k<=33)
        {
            unsigned double long fact=1;
            fact=1;
            for(int b=k;b>=1;b--)
            {
                    fact=fact*b;
            }
            cout<<"\nThe factorial of "<<k<<" is "<<fact<<"\n";
        }


        else
        {
            int numArr[10000];
            int total,rem=0,count;       
            register int i;              
            //int i;
            for(i=0;i<10000;i++)
            numArr[i]=0;             

            numArr[10000]=1;  
            for(count=2;count<=k;count++)   
            {
            while(i>0)
            {
                total=numArr[i]*count+rem;  
                rem=0;
                if(total>9)
                {
                    numArr[i]=total%10;
                    rem=total/10;
                }
                else
                {
                    numArr[i]=total;    
                }
                i--;             
            }
                rem=0;
                total=0;
                i=10000;
            }
            cout<<"The factorial of "<<k<<" is \n\n";
            for(i=0;i<10000;i++)            
                {
                    if(numArr[i]!=0 || count==1)  
                    {
                    cout<<numArr[i];
                    count=1;
                }
            }
            cout<<endl;
        }

        cout<<"\n\n";
    }//while
return 0;

}

Output:

![Large Factorial Calculator

Enter a number be calculated:250
The factorial of 250 is

32328562609091077323208145520243684709948437176737806667479424271128237475551112
09488817915371028199450928507353189432926730931712808990822791030279071281921676
52724018926473321804118626100683292536513367893908956993571353017504051317876007
72479330654023390061648255522488194365725860573992226412548329822048491377217766
50641276858807153128978777672951913990844377478702589172973255150283241787320658
18848206247858265980884882554880000000000000000000000000000000000000000000000000
000000000000][1]
Share:
11,871

Related videos on Youtube

Code Geas Coder
Author by

Code Geas Coder

Updated on September 15, 2022

Comments

  • Code Geas Coder
    Code Geas Coder over 1 year

    I am doing a factorial program with strings because i need the factorial of Numbers greater than 250

    I intent with:

    string factorial(int n){
        string fact="1";
        for(int i=2; i<=n; i++){
            b=atoi(fact)*n;
    
        }
    
    }
    

    But the problem is that atoi not works. How can i convert my string in a integer.

    And The most important Do I want to know if the program of this way will work with the factorial of 400 for example?

  • Mats Petersson
    Mats Petersson almost 11 years
    Thanks for the dump of the number - proves that the code I just wrote works.
  • FrankRuperto
    FrankRuperto almost 11 years
    @Jonathan - Are there other websites which can, for example, calculate 6!49 (i.e. factorial 49, 6 at a time) ?
  • Jonathan Leffler
    Jonathan Leffler almost 11 years
    @FrankComputer: I dunno. I forget which search I used, but that site came up top or second on the list when I did a simple Google search (maybe 'factorial calculator' — it came up second for me just now). I don't need to calculate factorials often enough to have an inventory of such sites.
  • Arun Kumar M
    Arun Kumar M over 9 years
    Is there unsigned double long in c++ ?