简体   繁体   中英

Sum of Squares Program in C with equation z = x^2 + y^2

I'm trying to write a program that finds and writes out all pairs of positive integers x and y such that x^2 + y^2 = z. I want to Prompt the user for the value z. x and y can either or both be zero. For example, if z is 25, then x and y are:

0, 5; 3, 4; 4, 3; 5, 0.

If no such x and y are found, the program writes out “None Found”. The program doesn't search through all possible integers to find x and y. For a given value of z it can stop searching for x when x^2 > z. (And the same for y.) I need to expect z to be zero up to a value called INT_MAX.

This is what I have so far:

#include<stdio.h>
#include<limits.h>

int main()
{   
 int x,y,z;

 printf("Enter a value for Z: \n");
 scanf("%d",&z);

 z = x*x + y*y;


 for(x = 0; x*x < z; ++x)
 {   
    printf("%d",&x);
 }

 for(y = 0; y*y < z; ++y)
{
printf("%d",&y); 
 }   

}

My issue is that I'm trying to write some sort of loop that will start both x and y equaling zero and then increment them up to form all the different possible combinations of x and y to get whatever the user inputs for Z. Here, I have them in two different for loops. I'm not sure what to do, any help would be appreciated.

#include<stdio.h>
#include<limits.h>

int main()
{   
    int x, y, z, zx, found;

    printf("Enter a value for Z: \n");
    scanf("%d", &z);


    found = 0;
    for(x = 0; x*x <= z; ++x)
    {
        zx = z - x*x;
        for(y = 0; y*y < zx; ++y);
        if (zx == y*y)
        {
            found += 1;
            printf("%d, %d;\t", x, y);
        }
    }

    if (found == 0)
        printf("No solutions have been found");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM