How to enumerate x^2 + y^2 = z^2 - 1 (with additional constraints)

前端 未结 6 2079
野性不改
野性不改 2021-02-04 09:33

Lets N be a number (10<=N<=10^5).

I have to break it into 3 numbers (x,y,z) such that it validates the following conditions.

6条回答
  •  一整个雨季
    2021-02-04 10:15

    No time to properly test it, but seemed to yield the same results as your code (at 100 -> 6 results and at 1000 -> 55 results).

    With N=1000 a time of 2ms vs your 144ms also without List

    and N=10000 a time of 28ms

    var N = 1000;
    var c = 0;
    
    for (int x = 2; x < N; x+=2)
    {
        for (int y = x; y < (N - x); y+=2)
        {
            long z2 = x * x + y * y + 1;
            int z = (int) Math.Sqrt(z2);
            if (x + y + z > N)
                break;
            if (z * z == z2)
                c++;
        }
    }
    
    Console.WriteLine(c);
    

提交回复
热议问题