01-16-2018, 10:52 PM
This mini-challenge is really is a cry for help improving a painfully inelegant-looking routine.
Suppose an integer X contains prime factors of 2^Y and 5^Z. What is MAX(Y,Z)?
Example: 2000 = 2^4 * 5^3, so Y=4 and Z=3, so MAX(Y,Z)=4.
The following ugly HP 50g routine takes X as its input and outputs MAX(Y,Z):
Input: 31250 (which = 2*5^6)
Output: 6 in 0.114 seconds
Input: 2^555
Output: 555 in 0.97 seconds
Input: 3^175
Output: 0 in 0.87 seconds
It works and it's fast enough, but it looks like a brutally hamfisted approach. It hurts my eyes. There MUST be a simple and elegant way of doing this. Thanks for any insights you can share!
Suppose an integer X contains prime factors of 2^Y and 5^Z. What is MAX(Y,Z)?
Example: 2000 = 2^4 * 5^3, so Y=4 and Z=3, so MAX(Y,Z)=4.
The following ugly HP 50g routine takes X as its input and outputs MAX(Y,Z):
Code:
<< FACTORS DUPDUP
IF 2 POS DUP THEN 1. + GET ELSE NIP END SWAP DUP
IF 5 POS DUP THEN 1. + GET ELSE NIP END MAX >>
Input: 31250 (which = 2*5^6)
Output: 6 in 0.114 seconds
Input: 2^555
Output: 555 in 0.97 seconds
Input: 3^175
Output: 0 in 0.87 seconds
It works and it's fast enough, but it looks like a brutally hamfisted approach. It hurts my eyes. There MUST be a simple and elegant way of doing this. Thanks for any insights you can share!