algorithm – Projecteuler.net Problem 2 using collection pipeline Pattern


I solve projecteuler.net Problem 2 deferent way

  1. Generate number from 1 to range ex 100 and get the even number
  2. Get Fibonacci numbers from list
  3. Reduce array

I have one problem with a large set of numbers like 1000000 or 4000000.
can I ask how can I optimize the code? I am trying to solve the problem as a collection pipeline

swift code

var evenArray = ((0..<100).filter({$0 % 2 == 0}))

print(evenArray)

var evenFibArray = evenArray.filter {(isPerfectSquare(5 * $0 * $0  + 4) || isPerfectSquare(5 * $0 * $0  - 4))}

print(evenFibArray)

var result  = evenFibArray.reduce(into: 0){$0 += $1}

print(result)


func isPerfectSquare(_ x:Int ) -> Bool {
    let i = Int(sqrt(Double(x)))
    return  (i * i == x)
}