'How can I tell if a number is a power of 10 in Kotlin or Java?

I have a number (64-bit int) and want to know if it's a pure power of 10. That is to say, a 1 followed 0 or more by zeroes. Is there an efficient way of doing this that does not involve turning it into a String?


Currently I'm doing this:

Kotlin

fun isPowerOf10(n: Long): Boolean {
    val logN = Math.log10(myInt.toDouble())
    return logN != Math.floor(logN)
}

Java

static boolean isPowerOf10(long n) {
    double logN = Math.log10((double) myInt);
    return logN != Math.floor(logN);
}

But it fails with isPowerOf10(999_999_999_999_999_999) (and the negative version), due to precision loss when converting to a double and taking log10, which outputs precisely 18.0.



Solution 1:[1]

This is another way you can check if the number is a power of 10. This code leverages on the fact that there are only a few numbers that fit in the long data type and are a power of 10.

public static boolean isPowerOfTen(long number){
        long[] powersOfTen = new long[] {
            1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, 1000000000000000000
        };

        return Arrays.binarySearch(powersOfTen, number) >= 0;
}

Solution 2:[2]

To make the second part of Rik Schaaf's answer in Kotlin, you could do something like:

val Long.isPowerOfTen get() = when (this) {
    1L,
    10L,
    100L,
    1000L,
    10000L,
    100000L,
    1000000L,
    10000000L,
    100000000L,
    1000000000L,
    10000000000L,
    100000000000L,
    1000000000000L,
    10000000000000L,
    100000000000000L,
    1000000000000000L,
    10000000000000000L,
    100000000000000000L,
    1000000000000000000L -> true
    else -> false
}

Solution 3:[3]

We can take help of String class

return Math.pow(10,String.valueOf(x).length()-1) == x;

Solution 4:[4]

Approach using String

boolean isPowerOfTen(int n){

  if( n == 1)
    return true;

  String num = String.valueOf(n);

  if(num.charAt(0) == '1' && Integer.parseInt(num.substring(1)) == 0)
      return true;
  else
     return false;
}

Here, we check whether the first char is '1' AND
whether the substring formed by discarding the first character is 0.

This won't work for 10^0, which is 1. Handled this case.

This is not as efficient as the Mathematical approach.

Solution 5:[5]

Also works for negative powers.

boolean isPowerOf10(long number) {
    while (number % 10 == 0 && number != 0) {
        number = number / 10;
    }
    return Math.abs(number) == 1;
}

Solution 6:[6]

This solution also works for negative powers. e.g 10 ^ -3

public boolean isPowerOfTen(double n) {
   return (Math.pow(10,Math.log10(n)) == n);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ruckus T-Boom
Solution 3 Bulat
Solution 4
Solution 5 michalavis
Solution 6