Skip to content

Instantly share code, notes, and snippets.

@PatrickVienne
Created September 30, 2023 19:07
Show Gist options
  • Save PatrickVienne/153bc7bf2b4796093803fec7658e8e0c to your computer and use it in GitHub Desktop.
Save PatrickVienne/153bc7bf2b4796093803fec7658e8e0c to your computer and use it in GitHub Desktop.
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