我正在解决问题的问题
Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
并且在我的解决方案下方。
class FB {
static func run () {
var a: UInt64 = 0
var b: UInt64 = 1
var c: UInt64 = 0
var sum: UInt64 = 0
for _ in 0..<4000000 {
print(c)
c = a + b // CRASHES
if c % 2 == 0 {
sum += c
}
a = b
b = c
}
print("SUM: , \(sum)")
}
}
但是它崩溃,如评论中所述线程1:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)
为了避免c
变量溢出我应该使用什么数据类型?>>
我正在解决问题的问题。斐波那契数列中的每个新术语都是通过将前两个术语相加而生成的。以1和2开头,前10个项将是:1、2、3、5、8,...
..其值不超过四百万美元
虽然Vadian的解决方案将按原样修复您的代码,但我想我将分享使用更实用的方法来解决此问题的方法。