You are viewing a single comment's thread. Return to all comments →
simple Swift version
import Foundation func calculateSum(num: Int) -> Void { var sum = 0 let numberDivided3 = (num - 1) / 3 let numberDivided5 = (num - 1) / 5 let numberDivided15 = (num - 1) / 15 sum = 3 * numberDivided3 * (numberDivided3 + 1) / 2 + 5 * numberDivided5 * (numberDivided5 + 1) / 2 - 15 * numberDivided15 * (numberDivided15 + 1) / 2 print(sum) } guard let t = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!) else { fatalError("Bad input") } for tItr in 1...t { guard let n = Int((readLine()?.trimmingCharacters(in: .whitespacesAndNewlines))!) else { fatalError("Bad input") } calculateSum(num: n) }
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #1: Multiples of 3 and 5
You are viewing a single comment's thread. Return to all comments →
simple Swift version