Created
August 28, 2015 17:56
-
-
Save damianesteban/ff9192c529d7102ea782 to your computer and use it in GitHub Desktop.
MultiplesOfThreeAndFive
This file contains 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
// 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