Created
September 30, 2023 19:07
-
-
Save PatrickVienne/153bc7bf2b4796093803fec7658e8e0c to your computer and use it in GitHub Desktop.
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
package towerofsevens | |
/* | |
Task: | |
Find a fast way, to determine whether a number (any int32 integer) is a power of 7? | |
Example Input and Output: | |
TowerOfSevens(-7) -> false | |
TowerOfSevens(0) -> false | |
TowerOfSevens(1) -> true | |
TowerOfSevens(7) -> true | |
TowerOfSevens(49) -> true | |
TowerOfSevens(21) -> false | |
TowerOfSevens(63) -> false | |
*/ | |
/* | |
Solution: | |
*/ | |
// greatest power of seven within the range of int32 | |
const sevenPowEleven = 1977326743 | |
// returns whether an integer number 'n' == 7**int(x) (a positive integer power of 7) | |
func TowerOfSevens(n int32) bool { | |
return n > 0 && sevenPowEleven%n == 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment