Created
May 24, 2017 07:24
-
-
Save witekbobrowski/0726410129eddfa159cddf3624c349fa to your computer and use it in GitHub Desktop.
HackerRank - 30 Days of Code - Day 19: Interfaces : Swift solution suggestion
This file contains hidden or 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
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