Project Euler #1 in Java

前端 未结 8 1619
旧时难觅i
旧时难觅i 2021-02-04 18:22

I\'m having problems with this code. I don\'t want to look at others, so I\'m wondering what\'s wrong with mine.

If we list all the natural numbers below 10 that are mu

8条回答
  •  情歌与酒
    2021-02-04 18:51

    Some certain conditions occurs where both conditions satisfies for 3 as well as 5 also. like when i=15 satisfies for both 15%3==0 and 15%5==0. so probably your answer is more than expected because you tried for both 3 and 5 separately. by doing this during these certain conditions you add repeated values. Hence it is better to check in single loop. like this -

        for(int i=0 ; i<1000 ; i++)
        {
            if(i%3==0 || i%5==0)
            {
                temp = temp + i;
            }
            System.out.println(temp);
        }
    

提交回复
热议问题