Java factorial calculation to get the number of code zero at the end of the result

  • 2020-04-01 02:31:07
  • OfStack

So when you see the problem, and you analyze it, 10 factorial is pretty big. It's not realistic to figure out the number of zeros at the end, and even then it's a hassle to implement.

Then I thought that the product of a factorial of some number with a 5 ending should produce a 0 at the end of the result.

Put it into practice, tested a few, no errors.

Post out, we have a look, there is a problem timely advice:



    public static void test2(int number){
        int count = number/5;
        System.out.println(count);
    }

And review the mathematical knowledge, the above calculation should be wrong.

I should have written it like this, every time I get a multiple of 5, I'm going to have a plus 1.


int x = 10000;
while (x>0)
{
      count = count + x / 5;
      x = x / 5;
}
System.out.println(count);


Related articles: