0

I'm trying to solve problem 2 of Project Euler:

"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."

When running the code it keeps stating EXC_BAD_INSTRUCTION. It seems this is due to the enormous number that it tries to sum when getting to the 4 million terms.

I have tried both a for in loop as well as a while in loop. See the code (also parts in comments for prior attempt)

func sumFibonacci(until n:Int) {
    var num1 = 0
    var num2 = 1
    var sumEven = 0
    var number = 0


// Attempted with for in loop - crashed
//    for _ in 1...n {
//        let sum = num1+num2
//        num1 = num2
//        num2 = sum
//
//        if sum % 2 == 0 {
//            sumEven += sum
//        }
while number < n {
    let sum = num1+num2
    num1 = num2
    num2 = sum

    if sum % 2 == 0 {
        sumEven += sum
    }
    number += 1
}

print("Sum even terms are \(sumEven)")
}

sumFibonacci(until: 4000000)

I would expect a result (either right or wrong), but I keep getting the stated error.

  • You don't want 4 million terms of the Fibonacci sequence, you want to stop when the latest term is larger than 4 million. – vacawama Jul 21 '19 at 12:32
  • What line does it crash on? – Sam Jul 21 '19 at 13:01
  • 1
    Possible duplicate of https://stackoverflow.com/questions/30441199/sum-of-fibonacci-term-using-functional-swift – Joakim Danielson Jul 21 '19 at 13:18
  • The joy/challenge of Project Euler is finding the solution yourself. With that in mind, I try to provide the minimal help to get the person unstuck and moving in the right direction. – vacawama Jul 21 '19 at 13:27
  • It gives an error on the final line (when the function is called). @Sam – Appelmoessi Jul 21 '19 at 13:42
  • @Appelmoessi, did you understand my comment? Don't process 4 million terms. Stop when the latest term exceeds 4 million. That happens quickly after about only 32 terms. – vacawama Jul 21 '19 at 13:49
  • 1
    Yes! I solved it, thank you vacawama - I changed one thing only. number += 1 into number = sum. I didn't read the question right it seems :) – Appelmoessi Jul 21 '19 at 14:38
  • `number = sum` works, but it has a bug. It considers the first term that is over 4 million. You got lucky because the first term over 4 million is odd so it doesn't affect your sum, but if that had been even, your sum would have been too big. – vacawama Jul 21 '19 at 14:57
  • Wouldn't changing the parameter to 3.999.999 fix that risk? – Appelmoessi Jul 21 '19 at 15:01
  • No, because sum is way over 4 million and you try to add it to sumEven and the loop doesn't check that until the next loop. What you really need to do is `break` out of the loop if the sum > 4 million. – vacawama Jul 21 '19 at 15:09
  • Okay so now I've changed it so that after the variables declaration, there is an if/else which checks if sum > 4 million, then breaks, else the rest of the equation occurs. That should do it right? – Appelmoessi Jul 21 '19 at 15:22
  • Sounds good. Test it with `sumFibonacci(until: 6)`. The answer should be `2`. – vacawama Jul 21 '19 at 15:31
  • It works, after I change the condition sum > 4 million to sum > n so it's conditional on the input parameter! – Appelmoessi Jul 21 '19 at 21:18

0 Answers0