Mathematically beautiful numbers

While playing a mental math game, you realize that the number k is mathematically beautiful.

You then realize that the number x can be mathematically beautiful if it is represented as a sum of a sequence where each element is a power of k and all the numbers in the sequence are different.

Task

Your task is to determine whether the number is mathematically beautiful.

Example:

Input:  n=91, k=3
Output: YES

Approach

Java

public class MathematicBeautifulNum {
    public static void main(String[] args) {
        long n = 91, k = 3;
        boolean ok = true;
        while (n > 0) {
            if (n % k > 1) {
                ok = false;
                break;
            }
            n /= k;
        }

        System.out.println(ok ? "YES" : "NO");
    }
}


No comments:

Post a Comment