Skip to content

Instantly share code, notes, and snippets.

@damianesteban
Created August 28, 2015 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damianesteban/ff9192c529d7102ea782 to your computer and use it in GitHub Desktop.
Save damianesteban/ff9192c529d7102ea782 to your computer and use it in GitHub Desktop.
MultiplesOfThreeAndFive
// Ghetto Way
var numbers : [Int] = []
func multiplesOfThreeAndFive(range1: Int, range2: Int) -> Int {
for i in range1..<range2 {
if i % 3 == 0 || i % 5 == 0 {
numbers.append(i)
}
}
let sumOfMultiples = numbers.reduce(0, combine: +)
return sumOfMultiples
}
multiplesOfThreeAndFive(1, range2: 1000)
// Fancy Pants Way
var newNumbers = Array((1..<1000))
let multiples = newNumbers
.filter { $0 % 3 == 0 || $0 % 5 == 0 }
.map { $0 }
.reduce(0, combine: +)
print(multiples)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment