# 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) }  • Is there really a problem with large numbers? Your code runs in less than 50 milliseconds for an upper bound of 4,000,000. Did you compile the code in “Release” mode so that it is optimized? – I am not saying that it cannot be improved and optimized further, but just want to be sure of your expectations. – Martin R Sep 11 '20 at 7:13 • I use playground – NinjaDeveloper Sep 11 '20 at 7:21 • Should I use the lazy property to improve the computing ? – NinjaDeveloper Sep 11 '20 at 7:24 • A Playground is suitable for interactive programming, but horribly slow. The code is not optimized, and values are displayed in the side bar for each computation step. If you care for performance, use a “real” Xcode Command Line Tool project. – Martin R Sep 11 '20 at 7:26 • I did not realize how bad the playground – NinjaDeveloper Sep 11 '20 at 7:32 ## 1 Answer updated my code to use a deferent approach and apply collection pipeline update my code 1. I use collection pipeline refactoring for more information about the topics look https://martinfowler.com/articles/refactoring-pipelines.html 2. I use stride function instead of filter to generate even Integer array Sidenote: I don't know if I will save a couple of computing cycles—all I want to make my code more Swifty. func isPerfectSquare(_ x:Int ) -> Bool { let i = Int(sqrt(Double(x))) return (i * i == x) } var result = (Array(stride(from: 0enter code here, to: 100, by: 2)).filter{(isPerfectSquare(5 *$0 * $0 + 4) || isPerfectSquare(5 *$0 * $0 - 4))}).reduce(into: 0){$0 += \$1}
print(result)

• You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process. – Vogel612 Sep 11 '20 at 14:55
• I update my code can you upvote me – NinjaDeveloper Sep 11 '20 at 15:06
• You have still neither provided a review of the original code nor describe why this new code is better than the original. In short, this is still an alternative solution, not a code review answer. You can use links to support reasoning for improvements, but you must include the salient details in the body of the answer, because links can, over time, break. Simply stating "I used xxx refactoring, for more information about xxx look {link}" is not a review. – AJNeufeld Sep 11 '20 at 22:35
• thank you for your advice. I am now profiling and benchmarking the console programme. right now I write how to solve the solution using higher-order functions – NinjaDeveloper Sep 12 '20 at 0:52