Project Euler: Problem 1 (Possible refactorings and run time optimizations)

前端 未结 13 2051
半阙折子戏
半阙折子戏 2020-12-11 08:35

I have been hearing a lot about Project Euler so I thought I solve one of the problems in C#. The problem as stated on the website is as follows:

If w

13条回答
  •  爱一瞬间的悲伤
    2020-12-11 09:02

    Here's a transliteration of my original F# solution into C#. Edited: It's basically mbeckish's solution as a loop rather than a function (and I remove the double count). I like mbeckish's better.

    static int Euler1 ()
    {
      int sum = 0;
    
      for (int i=3; i<1000; i+=3) sum+=i;
      for (int i=5; i<1000; i+=5) sum+=i;
      for (int i=15; i<1000; i+=15) sum-=i;
    
      return sum;
    }
    

    Here's the original:

    let euler1 d0 d1 n =
      (seq {d0..d0..n}       |> Seq.sum) +
      (seq {d1..d1..n}       |> Seq.sum) -
      (seq {d0*d1..d0*d1..n} |> Seq.sum)
    
    let result = euler1 3 5 (1000-1)
    

提交回复
热议问题