0

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.

JD1997
  • 65
  • 7

1 Answers1

3
#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");
}
AGN Gazer
  • 7,569
  • 2
  • 21
  • 43
  • Works like a charm! Thank you! – JD1997 Oct 01 '18 at 01:54
  • 1
    When `z` is large, `x*x < z` can `int` overflow which is undefined behavior (UB) and can lead to an infinite loop. `for(x = 0; x || x < z/x; ++x)` does not have that problem. Similar weakness with `y*y < zx`. – chux - Reinstate Monica Oct 01 '18 at 01:57
  • @chux Thanks for comments! I fixed `<=` point. The overflow issue I'll leave here as comment: my intention was to provide an outline to fix broken logic in OP example. Your comments are valid and should be taken in account by OP. – AGN Gazer Oct 01 '18 at 02:17
  • The inner loop doesn't actually have to start at 0. Without restriction we can assume X >= y and then any solution we find, we just also print the mirror solution (swap x and y) unless both numbers are equal. – Ingo Bürk Oct 01 '18 at 02:25
  • 1
    Also, as a note @JohnDaniewicz, there are other more performant algorithms to solve this problem, see, e.g., https://stackoverflow.com/a/4720397/8033585 My code here is the simplest fix for what I thought was OP _intention_. – AGN Gazer Oct 01 '18 at 02:26
  • @IngoBürk Yes, that is true and I was thinking about the same thing. Usually people want _unique pairs_ and not swapped values (in addition to original). But I will leave optimization to OP (or, you can post your own answer). – AGN Gazer Oct 01 '18 at 02:28
  • I actually just noticed that the code does not give me 5,0 also as an answer, which would just be the mirror of 0,5. Yet it gives me both 3,4 and 4,3. – JD1997 Oct 02 '18 at 03:49
  • 1
    @JohnDaniewicz Fixed in the last edit: the innermost loop should have had _strict_ inequality. – AGN Gazer Oct 02 '18 at 04:55
  • Good, but an explanation in English would be welcome – Basile Starynkevitch Oct 02 '18 at 04:57