Skip to content

Instantly share code, notes, and snippets.

@witekbobrowski
Created May 24, 2017 07:24
Show Gist options
  • Save witekbobrowski/0726410129eddfa159cddf3624c349fa to your computer and use it in GitHub Desktop.
Save witekbobrowski/0726410129eddfa159cddf3624c349fa to your computer and use it in GitHub Desktop.
HackerRank - 30 Days of Code - Day 19: Interfaces : Swift solution suggestion
import Foundation
protocol AdvancedArithmetic {
func divisorSum(_ n: Int) -> Int
}
class Calculator: AdvancedArithmetic {
func divisorSum(_ n: Int) -> Int {
var result = n
for divisor in 1..<n {
result += n % divisor == 0 ? divisor : 0
}
return result
}
}
let myCalculator: AdvancedArithmetic = Calculator()
if let calculator = myCalculator as? AdvancedArithmetic {
print("I implemented: AdvancedArithmetic")
print(calculator.divisorSum(Int(readLine()!)!))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment