Created
April 17, 2022 22:00
-
-
Save Turskyi/4f25f1d6c0ca617e1d995f7ff8ed2a76 to your computer and use it in GitHub Desktop.
Return whether or not 'n' is a power of three.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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