e = Σ_{n=0}^∞ 1/n! = 1 + 1/1! + 1/2! + ...,故級數和趨近 e - 1 ≈ 1.718281828。
term_n = term_{n-1} / n,避免重複計算階乘。
#includeint main() { double sum = 0.0; double term = 1.0; // 1/0! = 1 for (int n = 1; n <= 20; n++) { term /= n; // term = 1/n! sum += term; // 累加 } printf("%.10f\n", sum); // 1.7182818285 return 0; }
t = t * (1/n); // 錯誤:1/n 整數除法 = 0 (n>1) -> t 永遠歸零 // 正確: t = t / n; 或 t *= 1.0/n;
double sum = 0, fact = 1;
for (int n = 1; n <= 20; n++) {
fact *= n; // n!
sum += fact; // Σ n!
}
printf("%.0f\n", sum); // 2561327455189073900 (超過 double 53bit 精確整數範圍,末位不準)
20! = 2.43e18,double 尾數 53 bit ≈ 15-16 位十進位精度,Σ n! 末位會丟失精度。unsigned __int128 (GCC/Clang) 或大數庫。