Is there any fast way to find the largest power of 10 smaller than a given number?
I'm using this algorithm, at the moment, but something inside myself dies anytime I see it:
10**( int( math.log10(x) ) ) # python
pow( 10, (int) log10(x) ) // C
I could implement simple log10
and pow
functions for my problems with one loop each, but still I'm wondering if there is some bit magic for decimal numbers.
If we are asked to find the highest power of a number 'n' in a factorial, where n is a prime number, we can directly divide the factorial number by 'n' (and possible powers of 'n') to find the instances of 'n', and subsequently the highest power of 'n'.
Similarly, P(N,r)=N! (N−r)!. Hence, the largest power of a prime, dividing P(N,r) is given by sp(N!)
An alternative algorithm is:
i = 1;
while((i * 10) < x)
i *= 10;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With