The equation to which you want to find the roots is:
x * y = 1700/57
You need, for your solution space, two real numbers x
and y
. But x
and y
are not any real numbers, they are real positive, and can be represented by the fraction of two integers a/b
, being 1 <= a,b <= 300
. Note also that x = a_x/b_x
and that y = a_y/b_y
, there being no equality or difference relation between a_x, b_x, a_y, b_y
, the four integer variables being independent of each other.
This means that:
Sinceweareguaranteedthata_x,b_x,a_y,b_y
areintegersbetween1and300(closed),thismeanstwothings:
a_x*a_y=1700b_x*b_y=57Whatbasicallyrestrictsitselftofindingtwodivisorsof1700,bothbetween1and300,andtwodivisorsof57between1and300.
Dividerof57
Tofindallpairsofdivisorsof57between1and300,weneedtocheckwhichnumbersinthatrangedivide57andthencheckifyourcounterpointislessthan300.
I'mnotgoingtodemonstrate,butjustsearchforintegersinthe[1,57]
rangeforthevariableb_x
,andthenb_y=57/b_x
.Thereisnoadditionalrequirementtocheckb_y
.
Codetofindallfactorsb_x
:
intbx_candidato;for(bx_candidato=1;bx_candidato<=57;bx_candidato++){if(57%bx_candidato==0){printf("b_x %d, b_y %d\n", bx_candidato, 57/bx_candidato);
}
}
Storing the results in a vector is for a later time
Dividers of 1700
The idea is the same as the divisors of 57, but here it is necessary to check if a_y <= 300
. It is also necessary to search only within the [1,300]
range, as it has no math artifice to narrow the search scope.
Therefore:
int ax_candidato;
for (ax_candidato = 1; ax_candidato <= 300; ax_candidato++) {
if (1700 % ax_candidato == 0 && 1700/ax_candidato <= 300) {
printf("a_x %d, a_y %d\n", ax_candidato, 1700/ax_candidato);
}
}
Storing the results in a vector is for a later time
Resolving the issue
Note that finding a_x
implies that there is only a single a_y
, as well as b_x
and b_y
.
The crux of your problem you can find here:
int divisores_1700[300];
int divisores_encontrados_1700 = 0;
int divisores_57[57];
int divisores_encontrados_57 = 0;
int ax_candidato;
int bx_candidato;
for (bx_candidato = 1; bx_candidato <= 57; bx_candidato++) {
if (57 % bx_candidato == 0) {
divisores_57[divisores_encontrados_57] = bx_candidato;
divisores_encontrados_57++;
}
}
for (ax_candidato = 1; ax_candidato <= 300; ax_candidato++) {
if (1700 % ax_candidato == 0 && 1700/ax_candidato <= 300) {
divisores_1700[divisores_encontrados_1700] = ax_candidato;
divisores_encontrados_1700++;
}
}
On top of the values found, any combination of divisores_57
with divisores_1700
addresses the problem. To find all these combinations:
int i, j;
for (i = 0; i < divisores_encontrados_57; i++) {
for (j = 0; j < divisores_encontrados_1700; j++) {
int a_x, a_y, b_x, b_y;
a_x = divisores_1700[j];
a_y = 1700/a_x;
b_x = divisores_57[i];
b_y = 57/b_x;
printf("(%d/%d) * (%d/%d) == 1700/57\n", a_x, b_x, a_y, b_y);
}
}
See working at ideone .