How to check if a number is a power of 5

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?

  • First check if the last digit has 5.
  • If the last digit is 5; divide it by 5.
    If the result of the division is 1, then the number is 5. Once again, check whether the result of the division is the force 5 itself (that is, go to step 1 with the result as a number).
+4
source share
4 answers

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

+9
source

int/long range , ( 60 ), HashSet, O (1)

+4

, , 5, , 1, . log_5 (n) , O (lg n), . 9094947017729282379150390625 40 !

+2

, 5. , , , .

, n, , , .

0

All Articles