Skip to content

Instantly share code, notes, and snippets.

@Turskyi
Created April 17, 2022 22:00
Show Gist options
  • Save Turskyi/4f25f1d6c0ca617e1d995f7ff8ed2a76 to your computer and use it in GitHub Desktop.
Save Turskyi/4f25f1d6c0ca617e1d995f7ff8ed2a76 to your computer and use it in GitHub Desktop.
Return whether or not 'n' is a power of three.
/*
Given an integer n, return true if it is a power of three. Otherwise, return false.
An integer n is a power of three, if there exists an integer x such that n == 3x.
Example 1:
Input: n = 27
Output: true
Example 2:
Input: n = 0
Output: false
* */
fun isPowerOfThree(n: Int): Boolean {
return if (n == 1 || n == 3) {
true
} else if (n == 0 || n % 3 != 0) {
false
} else {
isPowerOfThree(n / 3)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment