Skip to content

Instantly share code, notes, and snippets.

@richimf
Created November 22, 2018 21:49
Show Gist options
  • Save richimf/743533a385a034e5eae686386ee33364 to your computer and use it in GitHub Desktop.
Save richimf/743533a385a034e5eae686386ee33364 to your computer and use it in GitHub Desktop.
Sum numbers 1 to n
//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