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

前端 未结 6 2078
野性不改
野性不改 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:13

    #include
    #include
    int main()
    {
        int N = 10000;
        int c = 0;
        for (int x = 2; x < N; x+=2)
        {
            for (int y = x; y < (N - x); y+=2)
            {
                auto z = sqrt(x * x + y * y + 1);
                if(x+y+z>N){
                    break;
                }
                if (z - (int) z == 0)
                {
                    c++;
                }
            }
        }
        std::cout<

    This is my solution. On testing the previous solutions for this problem I found that x,y are always even and z is odd. I dont know the mathematical nature behind this, I am currently trying to figure that out.

提交回复
热议问题