How to check if a number is power 5?
I could think of a downstream algorithm. Is there any way to improve it? Any math trick?
You do not need to look at individual numbers, you can just do it like this:
n = (int)(log(x) / log(5)); // get n = log5(x), truncated to integer if (pow(5, n) == x) // test to see whether x == 5^n // x is a power of 5
LIVE DEMO
int/long range , ( 60 ), HashSet, O (1)
, , 5, , 1, . log_5 (n) , O (lg n), . 9094947017729282379150390625 40 !
9094947017729282379150390625
, 5. , , , .
, n, , , .