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}))


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


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


func isPerfectSquare(_ x:Int ) -> Bool {
    let i = Int(sqrt(Double(x)))
    return  (i * i == x)
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Martin R Sep 11 '20 at 7:13
  • \$\begingroup\$ I use playground \$\endgroup\$ – NinjaDeveloper Sep 11 '20 at 7:21
  • \$\begingroup\$ Should I use the lazy property to improve the computing ? \$\endgroup\$ – NinjaDeveloper Sep 11 '20 at 7:24
  • \$\begingroup\$ 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. \$\endgroup\$ – Martin R Sep 11 '20 at 7:26
  • \$\begingroup\$ I did not realize how bad the playground \$\endgroup\$ – NinjaDeveloper Sep 11 '20 at 7:32

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: 0`enter code here`, to: 100, by: 2)).filter{(isPerfectSquare(5 * $0 * $0  + 4) || isPerfectSquare(5 * $0 * $0  - 4))}).reduce(into: 0){$0 += $1}
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Vogel612 Sep 11 '20 at 14:55
  • \$\begingroup\$ I update my code can you upvote me \$\endgroup\$ – NinjaDeveloper Sep 11 '20 at 15:06
  • \$\begingroup\$ 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. \$\endgroup\$ – AJNeufeld Sep 11 '20 at 22:35
  • \$\begingroup\$ 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 \$\endgroup\$ – NinjaDeveloper Sep 12 '20 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.