Skip to content

Instantly share code, notes, and snippets.

@m4scosta
Created December 2, 2015 09:39
Show Gist options
  • Save m4scosta/9883b91e1a96b6a1d088 to your computer and use it in GitHub Desktop.
Save m4scosta/9883b91e1a96b6a1d088 to your computer and use it in GitHub Desktop.
infix operator ** {}
func ** (n: Int, p: Int) -> Int {
return Int(pow(Double(n), Double(p)))
}
func splitInt(n: Int) -> [Int] {
if n < 10 {
return [n]
}
return splitInt(n / 10) + [n % 10]
}
func isArmstrongNumber(n: Int) -> Bool {
let nums = splitInt(n)
let size = nums.count
var sum = 0
for i in nums {
sum += (i ** size)
}
return sum == n
}
print(isArmstrongNumber(100))
print(isArmstrongNumber(153))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment