阶乘结束时的零数

时间:2014-03-08 14:12:58

标签: c# numbers

我需要在阶乘数的末尾找到零的数量。所以这是我的代码,但它不起作用:/

using System;

class Sum
{
    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine());
        long factoriel = 1;

        for (int i = 1; i <= n; i++)
        {
            factoriel *= i;
        }

        Console.WriteLine(factoriel);

        int timesZero = 0;

        while(factoriel % 10 != 0)
        {
            timesZero++;
        }
        Console.WriteLine(timesZero);
    }
}

我知道我可以使用for loop除以5,但我不想这样做。我的代码中的问题在哪里?为什么它不起作用?

5 个答案:

答案 0 :(得分:4)

您的算法存在问题整数溢出。想象一下,你得到了

  n = 1000

所以n! = 4.0238...e2567;你不应该计算n!但要计算(5**p)*m形式的术语,其中p和m是整数:

  5 * m gives you one zero
 25 * m gives you two zeros
625 * m gives you three zeros etc

最简单的代码(大n上很慢)是

  static void Main(string[] args) {
    ...
    int timesZero = 0;

    for (int i = 5; i <= n; i += 5) {
      int term = i;

      while ((term % 5) == 0) {
        timesZero += 1;
        term /= 5;
      }
    }
    ...
  }

实施速度快得多

  static void Main(string[] args) {
    ...
    int timesZero = 0;

    for (int power5 = 5; power5 <= n; power5 *= 5) 
      timesZero += n / power5;

    ...
  }

答案 1 :(得分:0)

在Factorial中计算尾随零

static int countZerosInFactOf(int n)##
{   
    int result = 0;
    int  start = 1;
    while (n >= start)
    {
        start *= 5;
        result += (int)n/start; 
    }
    return result;
} 

答案 2 :(得分:0)

确保添加内置参考 System.Numeric

using System.Text;
using System.Threading.Tasks;
using System.Numeric

namespace TrailingZeroFromFact
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a no");
            int no = int.Parse(Console.ReadLine());
            BigInterger fact = 1;
            if (no > 0)
            {
                for (int i = 1; i <= no; i++)
                {
                    fact = fact * i;
                }
                Console.WriteLine("{0}!={1}", no, fact);
                string str = fact.ToString();
                string[] ss = str.Split('0');
                int count = 0;
                for (int i = ss.Length - 1; i >= 0; i--)
                {
                    if (ss[i] == "")
                        count = count + 1;
                    else
                        break;
                }
                Console.WriteLine("No of trailing zeroes are = {0}", count);
            }
            else
            {
                Console.WriteLine("Can't calculate factorial of negative no");
            }
            Console.ReadKey();
        }
    }
} 

答案 3 :(得分:0)

static void Main(string[] args)
        {
            Console.WriteLine("Enter the number:");
            int n = int.Parse(Console.ReadLine());
            int zero = 0;
            long fac=1;
            for (int i = 1; i <= n; i++)
            {
                fac *= i;
            }
            Console.WriteLine("Factorial is:" + fac);
        ab:


        if (fac % 10 == 0)
        {
            fac = fac / 10;
            zero++;
            goto ab;
        }
        else
        {
            Console.WriteLine("Zeros are:" + zero);
        }
        Console.ReadLine();
        }

答案 4 :(得分:0)

你的代码似乎很好,只是在while条件下稍作修正:

        public static int CalculateTrailingZeroes(BigInteger bigNum)
    {
        int zeroesCounter = 0;

        while (bigNum % 10 == 0)
        {
            zeroesCounter++;
            bigNum /=10;
        }

        return zeroesCounter;
    }

这很有效,我刚试过它。