【发布时间】:2021-05-28 13:37:03
【问题描述】:
// I am attempting to answer the following question:
如果我们列出所有小于 10 且是 3 或 5 倍数的自然数,我们会得到 3、5、6 和 9。这些倍数之和是 23。
//Find the sum of all the multiples of 3 or 5 below 1000.
// function creating both integers, max number, and filtering
func sumOfMultiples(x: Int, y: Int, n: Int) {
var numberList: [Int] = []
for i in 0..<y {
if x < n && y < n && x % n == 0 || y % n == 0 {
return numberList.append(i)
}
}
}
sumOfMultiples(x: 3, y: 5, n: 1000)
【问题讨论】:
-
不要使用 return
return。你在哪里求和? -
解决PE #1?
-
要求记录所有号码的列表吗?如果不是,您可以在每次循环时继续添加“总和”并返回它
-
您能否详细说明需要在何处以及为什么需要附加一个空数组?最坏的情况是你可以声明一个 [Any] 类型的数组,然后只需 append([])
-
@MartinR 是的!!!我目前正在通过 exercism.io 工作