-1
我試圖運行一個程序,對大數的階乘進行某些操作(比如說50!;即3.041e + 64 - 巨大!),因此不適合在我知道的正常int數據類型中(無符號long long int等)在C程序中使用適當的數據類型
我使用哪種數據類型來存儲這些值?
P.S我試圖在階乘中查找尾隨零。以下是我的方法:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int FactorialFinder(int a)
{
if (a>1)
a= a* (FactorialFinder(a-1)) ;
return a;
}
int main()
{
printf("Enter number \n");
int num ;
scanf("%d",&num) ;
printf("number is %d\n",num);
printf("Factorial is %d",(num = FactorialFinder(num))) ;
int x=0, count = 0 ;
while(num>0)
{
x = (num%10) ;
if (x == 0)
count++ ;
else
break;
num= num/10 ;
}
printf("\nNumber of trailing zeroes is %d",count) ;
getchar() ;
return 0;
}
工作正常12!結果是錯誤的(從17!它開始返回負因子值(?),從34!它給0)我猜是由於數據類型問題。有人可以幫我嗎?
'n!'的小數表示形式中的尾隨零數由factorial中的'5'的因子數決定。你不需要計算完整的階乘。 – EOF
你可以用一個bignum庫蠻力,但如果你從這個地方得到更多的問題,用bignum實現樸素的算法不太可能解決你所有的問題。 – user2357112
@EOF我知道這種方法,這就是爲什麼我沒有要求解決方案。有沒有使用我的方法的方法? –