staticvoidMain(string[] args) { Program p = newProgram(); p.GetFactorial(26); DoMath d = newDoMath(p.HowManyZero); d += p.HowManyZero2; d(26); Console.Read(); }
publiclongHowManyZero(long num)//第一种解法 { long result = 0; for (long i = 1; i <= num; i++) { long j = i; while (j % 5 == 0) { result++; //检查每一个数可以分解因式出多少个5 j /= 5; } }
Console.WriteLine(result); return result; }
publiclongHowManyZero2(long num)//第二种 { long ret = 0; while (num != 0) { ret += num / 5; //可以被5整除,然后是25、125、625...... num /= 5; }
Console.WriteLine(ret); return ret; }
public decimal GetFactorial(long num)//获得阶乘 { decimal ret = 1; for (long i = 1; i <= num; i++) { ret *= i; }