Jump to content

help with factorial of large numbers

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Chinmoy

Chinmoy

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 392 posts
in this prog. i am trying to implement the factorial but for large numbers.mnow we all know that java has this long long data type for huge numbers but in c i have tried to implement this as we would have done a multiplication on paper.multiplying the first bits with the number to multiply...
so the code is here

[HIGHLIGHT="C++"]
#include<iostream.h>
#include<math.h>
void main()
{
int q,arext,a,carry=0,c,num,prod,arr[40],sum,ext;
cout<<"Enter the number to find the factorial\n";
cin>>num;
for(a=0;a<40;a++)
{
arr[a]=-1;
}
for(a=1;a<num;a++)
{
c=39;
arext=0,q=39;
while(arr[q]!=-1)
{
arext+=arr[q]*(pow(10,(39-q)));
q--;
}
cout<<" arext="<<arext<<" a="<<a<<"\n";
if(a==1)
{ prod=a*(a+1); }
else
{ prod=arext*(a+1); }
while(prod>=1)
{
ext=prod%10;
ext+=carry;
prod/=10;
cout<<"\nVal="<<ext<<" arr[c]="<<arr[c]<<endl;
if(arr[c]==-1)
{
arr[c]=ext;
}
else if(arr[c]!=-1 && (arr[c]+ext)<10)
{
cout<<"\nB4 "<<arr[c];
arr[c]=ext;
cout<<"\nAftr "<<arr[c];
}
else if(arr[c]!=-1 && (arr[c]+ext)>10)
{
sum=arr[c]+ext;
cout<<"/nSum="<<sum;
arr[c]=sum%10;
carry=sum/10;
cout<<"/nCarry="<<carry;
}
c-=1;
}
}
for(a=0;a<40;a++)
{
if(arr[a]!=-1)
cout<<arr[a];
}
}
[/HIGHLIGHT]
burt its not working somehow!can you point out the error!

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Adding some comments would help us to look at the code and understand what it's doing. Also, is it not working for certain values, or just not working at all?

Also, since 0! = 1, I would make sure you seed your array to represent 1 as the default value.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Chinmoy

Chinmoy

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 392 posts
ok.ill try it out and let you know.and the next time i post ill try to give the comments as well..

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
There are some easier ways to go about this. One is to use recursion:


//use qwords to hold huge ints

unsigned __int64 factorial(unsigned __int64 num)

{

    //base case - implemented using recursion

    if(num == 0)

        return 1;

    else

        //recursive call

        return num * factorial(num - 1);

}


The other is to use iteration. For absolutely huge numbers, this might be a better idea.


unsigned __int64 factorial(unsigned __int64 num)

{

    unsigned __int64 total = 1;

    while(num > 0)

    {

        total *= num;

        --num;

    }

    return total;

}



#5
Chinmoy

Chinmoy

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 392 posts
in which compiler can we use this unsigned__int?also which data type can actually hold that large a number?say i want to try out factorial 25!

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,720 posts
You can use it in Microsoft compilers. For others, just use long long. It's not guaranteed to be 64 bits like __int64, but it's worth a try.

32-bit unsigned integers can hold values from 0 to 4,294,967,295; they're usually declared as unsigned int, but in older compilers an int is only 16 bits.

64-bit unsigned integers can hold values from 0 to 18,446,744,073,709,551,615. Depending on the compiler, they're usually declared as either unsigned long or unsigned long long.

Microsoft extensions __int8, __int16, __int32, and __int64 are guaranteed to always have the number of bits specified. The problem is, they're not universally used.

Make sure you declare your variable holding the factorial as unsigned. Signed numbers have half the capacity because they use a bit to indicate sign, but since a positive factorial can never be negative, just use unsigned integers for greater range.