Skip to content

Instantly share code, notes, and snippets.

@bhakes
Last active November 8, 2018 17:42
Show Gist options
  • Save bhakes/a203998f2873aaefbc9d21e6516c1844 to your computer and use it in GitHub Desktop.
Save bhakes/a203998f2873aaefbc9d21e6516c1844 to your computer and use it in GitHub Desktop.
import UIKit
func sumOf35Multiples(_ number: Int) -> Int {
// Check to make sure some joker didn't put zero or negative
// or a really big number. return -1 if it might be a joker
guard number > 0, number < 100000 else { return -1 }
// instantiate local vars
var matches: Array<Int> = []
var sum = 0
// check if the number mod 3 or mod 5 is zero
// if so, add to the matches list
for index in 1..<number {
if(index % 3 == 0 || index % 5 == 0){
matches.append(index)
}
}
// sum the values in matches int array
for match in matches {
sum += match
}
return sum
}
let testCases = [10,-1,2,200,30,-2,1000,200000]
for test in testCases{
print(sumOf35Multiples(test))
}
print("The sum below 1000 is equal to: \(sumOf35Multiples(1000))")
@dillon-mce
Copy link

Looks great Ben! I like how you've solved the problem and how much of your thought process I can see. I also like that you've bounded it a little bit, in case someone tries to use too big or too small of a number. If this were a real implementation somewhere, I would just suggest that you document why you might sometimes return a -1, so it doesn't break some program that relies on sumOf35Multiples(100001). You could also create the lower bound by using UInt instead of Int and starting your for loop at 0. Overall though, great job!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment