Helllo Guy's
I was thinking of this problem for the past one month. We have to generate the factoroial of say 100 using c and usual integer data type how can this be achived
thanx in advance
Printable View
Helllo Guy's
I was thinking of this problem for the past one month. We have to generate the factoroial of say 100 using c and usual integer data type how can this be achived
thanx in advance
int main(){
int x=0;
int y=100;
int total;
while(x<=y){
total *= x;
x++;
}
printf("The value is %i",total);
return 0;
}
Might work. But I make no guarantees. Might be hard to calculate 100! with a 32 bit integer though. It's like 9.3*10^157 or something like that.
You could also use a recursive algorithm
int factorial(int x){
static int total=0;
total*=x;
x--;
factorial(x);
return 0;
}
But I suck at writing recursive algorithms.
And please stop asking us to do your homework for you. We have better things to do.
If it's really like 9.3*10^157 or something like that, then it WON'T work. The maximum unsigned 32 bit integer is 4.3 x 10^9. The maximum unsigned 64 bit integer is 1.8 x 10^19. You will need a (dynamically allocated) string to store the result.Quote:
Might work. But I make no guarantees. Might be hard to calculate 100! with a 32 bit integer though. It's like 9.3*10^157 or something like that.
I remember my friend at univ wrote a program to calculate 200! using string. But I forget how. Don't care actually. What's the purpose anyway, other than doing your homework...
Peace always,
<jdenny>
sorry guys but you've got your math messed up over there.
'total' will never go beyond zero if you use those routines.
vozhan, so what would you use?
vozhan, Shriek never said it would work for sure:
Actually, it might go over 0, depending on how overflows are taken into account the integer would wrap around and start counting from 0 again (or a negative number like -(2 ^ 31) if you're using signed integers). ;)Quote:
Originally posted here by Striek
Might work. But I make no guarantees. Might be hard to calculate 100! with a 32 bit integer though. It's like 9.3*10^157 or something like that.
No way! Recursion is defiently not the way to figure this type of program, you will run out of memory very quickly. If you really need help, PM me and ill show you but i am very busy at the moment because of finals so i wont have time for the next like week so i dont know if ill be much help...Quote:
You could also use a recursive algorithm
int factorial(int x){
static int total=0;
total*=x;
x--;
factorial(x);
return 0;
}
vozhan is correct. With that algorithm, total will never get past 0 because of the following:
If you look above, you will notice that 0 has been assigned to total to start, then total is timed by x. Because total is zero, it doesn't matter what x is:Quote:
static int total=0;
total*=x;
total x "new total"
0 1 0
0 2 0
0 3 0
If you set total to 1 to start with, it might be alright...but it should be much better to use something like a while or a for loop rather than writing a recursive algorithm, if only because you should make your code as readable as possible unless you are entering the obsfucated c code competition :P
ac
[edit]the columns are a bit messed up in the "table" above, but you get the idea[/edit]
[edit]and you couldn't use a string to solve the problem simply because Hunt_guy said that he was to use the integer data type. If he was allowed to use a string, why not just use a long?[/edit]
One way to go about the solution would be to note the maximum size of an integer. Every time you reach this maximum size, you add one to a counter and start your "total" integer again (well not again, from wherever you got to). When you have worked out the whole factorial, you simply use output it to the screen:
std::cout << (counter*MAX_INT_SIZE)+total << endl;
or use printf (I don't want to make a fool of myself attempting to write it using printf, I never got used to the %s, %d, etc...never know which one to use :P
Anyhow, that would be a simple way of doing it.
ac
[edit] I just realised that that would not be entirely correct in that it assumes that you would get exactly the max int value every time, but it is almost right :P [/edit]
Erm, maybe you better ignore my last post. I've thought about it and I don't think that my maths is anywhere near correct :(. Hrrm...
Must be too late at night.
ac