Created
December 2, 2015 09:39
-
-
Save m4scosta/9883b91e1a96b6a1d088 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
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