Skip to content

Instantly share code, notes, and snippets.

@davidseek
Created June 8, 2020 19:40
Show Gist options
  • Select an option

  • Save davidseek/110d813df86ba7d120bb21d13fa660a7 to your computer and use it in GitHub Desktop.

Select an option

Save davidseek/110d813df86ba7d120bb21d13fa660a7 to your computer and use it in GitHub Desktop.
func isPowerOfTwo(_ n: Int) -> Bool {
// 1
guard n != 1 && n != 2 else {
return true
}
// 2
var last: Int = 2
// 3
for index in 2...Int.max {
// 4
let current = last * 2
// 5
if current == n {
return true
}
// 6
if current > n {
return false
}
// 7
last = current
}
// 8
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment