Home:ALL Converter>Find (x,y) solutions to equation

Find (x,y) solutions to equation

Ask Time:2019-01-04T03:31:03         Author:Ortal

Json Formatter

The program receives a positive number k from the user and should check how many solutions there are to the equation

3*x+5*y=k

In case of many solutions the function takes the greater absolute value of |x-y| of all solutions. If there is only one solution it prints it. For example:

  • If the user types k=34 there are two solutions for (x,y): (3,5) and (8,2). Hence there are two solution the program calculates |8-2| and |3-5| and takes the greater one, 6.

  • If the user for example types k=8 there is only one solution, (1,1).

Sadly my teacher asked from me to use only loops, if and else statements. No recursion, no helper functions and no arrays. She also wants the program to be efficient so I cannot use a loop inside a loop.

I tried to write the code but the program does not respond. I defined counter to count number of possible solutions for the equation and distance the greater absolute value:

void check_how_many_solutions(int n) {
    int  y = 0, counter = 0, distance = 0, equation1 = 0, equation2 = 0, equation3 = 0; 
    while (equation3 <= n) {
        equation1 = (n - (5 * y)) / 3;
        equation2 = (n - (3 * equation1)) / 5; 
        equation3 = (3 * equation1) + (5 * equation2);
        if (equation3 == n) { 
            counter++;  
            if (fabs(equation1 - equation2) > distance)
                distance = fabs(equation1 - equation2);
        }
        y++;
    }   
    if (counter > 1)
        printf("The values of x and y are (%d,%d)\n", equation1, equation2); 
    else
        printf("The greater absolute value of |x-y| is %d\n", distance);
}

The code does run but with no result.

Author:Ortal,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/54028663/find-x-y-solutions-to-equation
max1000001 :

Here's a program I made that I believe answers the question you're asking. You just want to loop through the whole number values of x and check if the computed y is also a whole number. I also keep track of the total number of solutions found and the solution with the greatest distance between x and y, and I give the final answer according to these.\n\n#include <stdio.h>\n#include <math.h>\n#include <stdlib.h>\n\nvoid count_solns(double);\n\nint main(int argc, char *argv[])\n{\n if (argc == 2 && argv[1]) {\n int k = atoi(argv[1]);\n count_solns(k);\n }\n else {\n puts(\"Usage: count_solns <positive_integer>\");\n return 1;\n }\n return 0;\n}\n\nvoid count_solns(double k)\n{\n printf(\"Print positive integer solutions to 3x + 5y = %.3f\\n\", k);\n int solns_found = 0;\n int distance = -1;\n int final_x, final_y;\n double x, y;\n for (x = 0; x <= (k/3); x++) {\n y = (k-3*x)/5;\n if (y == floor(y)) {\n printf(\"Solution found at (x, y) == (%d, %d)\\n\", (int) x, (int) y);\n solns_found++;\n if (fabs(x-y) > distance) {\n final_x = x;\n final_y = y;\n }\n }\n }\n if (solns_found == 0)\n printf(\"No whole number solutions found for 3x + 5y = %.3f\", k);\n else if (solns_found == 1)\n puts(\"This is the only solution where x and y are both whole numbers\");\n else\n printf(\"The whole number solution with the highest distance between x and y is (x, y) == (%d, %d)\", final_x, final_y);\n return;\n}\n\n\nUsage looks like this:\n\n$ ./count_solns 70\nPrint positive integer solutions to 3x + 5y = 70.000\nSolution found at (x, y) == (0, 14)\nSolution found at (x, y) == (5, 11)\nSolution found at (x, y) == (10, 8)\nSolution found at (x, y) == (15, 5)\nSolution found at (x, y) == (20, 2)\nThe solution with the highest distance between x and y is (x, y) == (20, 2)\n",
2019-01-03T20:24:57
yy