如果我有点傻,请原谅我,但我最近才开始编程,在 Project Euler 上做问题 160 可能有点超出我的深度。我已经尝试解决它,但似乎在任何个人计算机上处理 1tn 数字都需要很长时间,所以我想我应该研究数学以找到一些捷径。
欧拉计划问题 160:
对于任何 N,令 f(N) 为 N! 中尾随零之前的最后五位数字。例如,
9!= 362880 所以 f(9)=36288 10!= 3628800 所以 f(10)=36288 20!= 2432902008176640000 所以 f(20)=17664
求 f(1,000,000,000,000)
新尝试:
#include <stdio.h>
main()
{
//I have used long long ints everywhere to avoid possible multiplication errors
long long f; //f is f(1,000,000,000,000)
f = 1;
for (long long i = 1; i <= 1000000000000; i = ++i){
long long p;
for (p = i; (p % 10) == 0; p = p / 10) //p is i without proceeding zeros
;
p = (p % 1000000); //p is last six nontrivial digits of i
for (f = f * p; (f % 10) == 0; f = f / 10)
;
f = (f % 1000000);
}
f = (f % 100000);
printf("f(1,000,000,000,000) = %d\n", f);
}
旧尝试:
#include <stdio.h>
main()
{
//This part of the programme removes the zeros in factorials by dividing by 10 for each factor of 5, and finds f(1,000,000,000,000) inductively
long long int f, m; //f is f(n), m is 10^k for each multiple of 5
short k; //Stores multiplicity of 5 for each multiple of 5
f = 1;
for (long long i = 1; i <= 100000000000; ++i){
if ((i % 5) == 0){
k = 1;
for ((m = i / 5); (m % 5) == 0; m = m / 5) //Computes multiplicity of 5 in factorisation of i
++k;
m = 1;
for (short j = 1; j <= k; ++j) //Computes 10^k
m = 10 * m;
f = (((f * i) / m) % 100000);
}
else f = ((f * i) % 100000);
}
printf("f(1,000,000,000,000) = %d\n", f);
}