Message Boards Message Boards

Counting Trailing Zeros Of Factorials

Posted 5 years ago
13 Replies

Amazing, and really impressive how decently Wolfram handles calculations of Random numbers with 16384 digits!

POSTED BY: Fabian Wenger
Posted 5 years ago

If you liked this piece and want to explore further, why not generalizing the TZF functions above to mixed radix representations or make your own variant of them to optimize code, memory or performance.

Done. I substituted recursion for table accumulation. There are three functions,one (stack) specialized for base 10 (prime=5), another (stack) for any prime radix, and the ultimate (tzf) to handle the mixed radii - modeled after your TZF5. I removed the test for min>0 since 0 divided by power is still 0.

stack[0] := 0;
stack[n_] := Quotient[n, 5] + stack[Quotient[n, 5]];

stack[0, r_] := 0;
stack[n_, r_] := Quotient[n, r] + stack[Quotient[n, r], r];

 tzf[n_, r_] := 
  Min[Quotient[stack[n, #[[1]]], #[[2]]] & /@ FactorInteger[r]];

The new functions are faster. Timing tests follow for TrailingZerosFactorial and the Ntz submission. TrailingZerosFactorial beats Ntz for very large values. The columns are the N of N!, time for function 1, time for function 2, verification that the values match, and finally the ratio of times.

testGrowth[TrailingZerosFactorial, tzf, 72, time -> 0.1, power -> 4, 
 size -> 10]

tests

enter image description here

To test Ntz I had to define a two-parameter version.

Ntz[m_] := Total[Table[Floor[m/5^n], {n, 1, Floor[Log[m]/Log[5]]}]]
Ntz[m_, r_] := Ntz[m];

Finally Ntz fails for any power of 5.

enter image description here

Test driver

testGrowth[p1_, p2_, radix_, 
  OptionsPattern[{time -> 0.2, power -> 8, size -> 10}]] := 
 Module[{n, n1, n2}, 
  Block[{$RecursionLimit = Infinity}, 
    Table[{(n = RandomInteger[10^2^i]) // 
       N, (n1 = 
         RepeatedTiming[p1[n, radix], OptionValue[time]])[[1]], (n2 = 
         RepeatedTiming[p2[n, radix], OptionValue[time]])[[1]], 
      n1[[2]] == n2[[2]], n1[[1]]/n2[[1]]}, {i, OptionValue[power], 
      OptionValue[power] + OptionValue[size]}]] // TableForm]
POSTED BY: Douglas Kubler
Posted 5 years ago

Here is my Function I use to calculate the number of trailing zeros of a Factorial, It doesn't actually calculate any factorials so is quick for very large factorials i.e. 10^100!

Ntz[m_] := Total[Table[Floor[m/5^n], {n, 1, Floor[Log[m]/Log[5]]}]]
POSTED BY: Paul Cleary

Very concise indeed, and fast too!

POSTED BY: Fabian Wenger
Posted 5 years ago

Ntz aborts when the input is a power of 5.

POSTED BY: Douglas Kubler
Posted 5 years ago

Very nice. I'm impressed by how the post mirrors your NB. Is there an easy (automatic) way to upload all the cells from a NB?

POSTED BY: Updating Name

Hi, I believe that is accomplished by the Wolfram team, in the beginning I only had some intro text and the notebook. So the credit goes to them.

POSTED BY: Fabian Wenger
Posted 5 years ago
POSTED BY: Douglas Kubler

This is awesome! Thank you so much for sharing. By the way, we have this newly launched Wolfram Function Repository which could be a great place for your cool TZF :-)

POSTED BY: Vitaliy Kaurov
POSTED BY: Fabian Wenger

As it often happens there is a very concise form for this calculation with built-in Wolfram functions: IntegerExponent[Factorial[n], b] but the optimised version in the notebook is considerably faster for large integers. I have however problems on submitting the fast version to the Function Repository. Check, preview and deploy work fine but when submitting I get "ResourceSubmit::apierr The parameter FunctionLocation is missing."

POSTED BY: Fabian Wenger

enter image description here - Congratulations! This post is now featured in our Staff Pick column as distinguished by a badge on your profile of a Featured Contributor! Thank you, keep it coming, and consider contributing your work to the The Notebook Archive!

POSTED BY: Moderation Team

Very useful for this problem:

https://projecteuler.net/problem=160

I had already solved it though ;-)

POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract