Created
November 22, 2018 21:49
-
-
Save richimf/743533a385a034e5eae686386ee33364 to your computer and use it in GitHub Desktop.
Sum numbers 1 to n
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
//You: | |
func sumFromOne(upto n: Int) -> Int { | |
var result = 0 | |
for i in 1...n { | |
result += i | |
} | |
return result | |
} | |
sumFromOne(upto: 10) | |
//The guy she told you not to worry about | |
func sumFromOne(upto n: Int) -> Int { | |
return (n + 1) * n / 2 | |
} | |
sumFromOne(upto: 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment