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

前端 未结 13 2045
半阙折子戏
半阙折子戏 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 08:57

    I like technielogys idea, here's my idea of a modification

    static int Euler1 () 
    { 
      int sum = 0; 
    
      for (int i=3; i<1000; i+=3) 
      {
        if (i % 5 == 0) continue;
        sum+=i; 
      }
      for (int i=5; i<1000; i+=5) sum+=i; 
    
      return sum; 
    } 
    

    Though also comes to mind is maybe a minor heuristic, does this make any improvement?

    static int Euler1 () 
    { 
      int sum = 0; 
    
      for (int i=3; i<1000; i+=3) 
      {
        if (i % 5 == 0) continue;
        sum+=i; 
      }
      for (int i=5; i<250; i+=5)
      {
        sum+=i;
      }
      for (int i=250; i<500; i+=5)
      {
        sum+=i;
        sum+=i*2;
        sum+=(i*2)+5;
      }
    
      return sum; 
    } 
    

提交回复
热议问题