Post Reply 
Mini-challenge: MAX(factors of 2 or 5)
01-17-2018, 04:03 AM
Post: #3
RE: Mini-challenge: MAX(factors of 2 or 5)
(01-16-2018 10:52 PM)Joe Horn Wrote:  There MUST be a simple and elegant way of doing this. Thanks for any insights you can share!

I would approach it based on the last digit:

if Z=Y+C, then:

2^Y*5^Z = 10^Y*5^C
or if there's more 2's:
Y=Z+D

2^Y*5^Z = 10^Z*2^D

In other words: The common amount of 2's and 5's is the number of trailing zeros. Trimming and counting the trailing zeros is the first step.
Once that's done, the resulting number is either 2^D or 5^C, so if the last digit is a 5, then divide by 5 until 1 while counting, or faster: from 1, multiply by 5 while counting until >= value. For large C it might be faster to use C=LN(5^C)/LN(5)
If the last digit is not 5, then it has to be a power of 2, and the same reasoning as before works D=LN(2^D)/LN(2).
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Mini-challenge: MAX(factors of 2 or 5) - Claudio L. - 01-17-2018 04:03 AM



User(s) browsing this thread: 1 Guest(s)