C# - for Loop Freezes at strange intervals

前端 未结 3 1935
猫巷女王i
猫巷女王i 2021-01-23 06:53

I am working on Problem 14 on Project Euler, and my code seems to freeze at random intervals for no apparent reason.

static void Main()
{
    int maxNum = 0;
            


        
3条回答
  •  日久生厌
    2021-01-23 07:25

    I've solved it in another way, by caching the result for each step, and I've found your problem. I doubt your program ever stops.
    The statement num = (3 * num) + 1 may overflow over Int32.MaxValue and result in a negative number and an infinite loop(?).
    In this case, you can solve the problem by using long for your x.

提交回复
热议问题