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

前端 未结 13 2044
半阙折子戏
半阙折子戏 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:20
    new List<int>{3,5}.SelectMany(n =>Enumerable.Range(1,999/n).Select(i=>i*n))
                      .Distinct()
                      .Sum()
    

    [Update] (In response to the comment asking to explain this algorothm) This builds a flattened list of multiples for each base value (3 and 5 in this case), then removes duplicates (e.g where a multiple is divisible, in this case, by 3*5 =15) and then sums the remaining values. (Also this is easily generalisable for having more than two base values IMHO compared to any of the other solutions I have seen here.)

    0 讨论(0)
提交回复
热议问题