Problem 292 - Factorial Trailing Zeroes

Back to General discussions forum

sam_bandara     2022-08-30 03:18:15

Hello again guys, This time I stucked in the problem 292. To calcualte the factorial I used builtin python function but it takes forever to calculate. Then I used Sterlin Approximate (Read a lot :-) ) to calculate and endup with "OverflowError: (34, 'Result too large')".

math.sqrt(2 * math.pi) * y ** (y + 0.5) * math.exp(-y)

2nd issue is convert the factorial values into n Base. I just used classical division method but it is not the right method to use since there are a lot of divisions and etc.

Could you please someone givi me a hint on how to and where to start?

Cheers, Sam.

Rodion (admin)     2022-08-30 07:47:34
User avatar

Sam, Hi!

This reminds me the story of a person who is selling a piece of forested land, and when asked if he counted trees and total amount of wood in it, feels surprised "how am I supposed to count trees in the forest???"

You are asked about trailing zeroes of factorial, not about factorial itself!

I used builtin python function but it takes forever to calculate

Really, when you see input suspiciously large, you may suspect (without trying) it is done exactly to make direct calculation difficult :)

Then I used Sterlin Approximate

This obviously can't help as it is approximate and you won't have exact trailing digits of a value, right :)

Could you please someone givi me a hint on how to and where to start?

Well, recollect that it is popular interview question, and in its original form (for 100! and decimal system) it is supposed to be solved without paper or computer, straight "in head". Try this and you most probably get upon the idea.

gardengnome     2022-08-30 09:57:00
User avatar

25 does not have a trailing zero (in base 10), 28 does not have one, but 25*28 = 700 got two trailing zeros. Think about why ...

sam_bandara     2022-09-02 03:57:01

Thanks guys, stupid me :-) I enlighted and went through the following 2 articals. Think, I found a way.

https://www.handakafunda.com/number-of-trailing-zeros/
https://math.stackexchange.com/questions/226868/what-is-the-number-of-trailing-zeros-in-a-factorial-in-base-b
Please login and solve 5 problems to be able to post at forum