Who can help me to solve this problem? The problem is attached to the post.
deceptikon 1,790
Gonbe 32
deceptikon
commented:
Cheesy or not, giving away the answer is unhelpful -3
What have you done so far? We require proof of effort before offering any help.
A cheesy way would be to brute force, you'd end up with something like this:
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
// The treshold mentioned in the exercise.
#define TRESHOLD (0.0001)
// Test number.
#define N (0.123456789)
int max (const int l, const int r);
bool solve (const double n, int* const a, int* const b, int* const c, int* const d);
int max(const int l, const int r)
{
return (l > r) ? l : r;
}
bool solve (const double n, int* const a, int* const b, int* const c, int* const d)
{
// Iterate over the possible 'a' values.
for ((*a) = 24; (*a) <= 100; (*a)++)
{
// The check for 24 <= b <= 100 is implicit due to a's bounds.
// Should make it explicit if you want the ranges as variables.
for ((*b) = max(73 - (*a), 24); (*b) <= 121 - (*a); (*b)++)
{
// iterate over the possible 'c' values.
for ((*c) = 24; (*c) <= 100; (*c)++)
{
// Same as for 'b' here.
for ((*d) = max(90 - (*c), 24); (*d) <= 134 - (*c); (*d)++)
{
// Found an answer. Should add a a treshold for accuracy due to floating points.
if (fabs(n - ((double)((*a) * (*c)) / ((*b) * (*d)))) <= TRESHOLD)
{
// Found the answer.
return true;
}
}
}
}
}
return false;
}
int main()
{
int a, b, c, d;
// Will get rounded.
printf("Trying to solve the equation for n ~= %.15f..\n", N);
if (solve(N, &a, &b, &c, &d))
{
printf("Solution found: (%d / %d) * (%d / %d) ~= %.15f\n", a, b, c, d, N);
}
else
{
printf("No solution found!\n");
}
return 0;
}
Doing it efficiently is more interesting though, wouldn't be quite sure how I'd do that. Your ideas so far would probably be good, assuming you spend more time pondering on it than I did.
I thouht about this problem but i couldnt find the answer.It was so hard for me.I need the solution...
Dear Gonbe,thanks alot for answering this question.I have to check it.
I'm no math whiz, but don't you have to find the range of possible answers for a,b,c,d, separately, in every equation, and then look at the range where all 4 variable's ranges, overlap?
Emphasis on the highest and lowest values for each variable, of course. Everything in between should be golden. (good)
Dear Adak i did this but in this way there's a lot of answers and it took a long time.
Well, it seems that the last equation is useless, because the first one already means that n - (a/b)*(c/d) == 0 .. The other thing is that there is no condition on n, so we just have to find a,b,c and d that match equations 2,3 and 4 - we also now that a,b,c,d are integers - So maybe it's dumb but i'd solve it this way :
#include <stdio.h>
int main(void) {
int a, b, c, d;
for(a=24; a<=100; a++) {
for(b=24; b<=100; b++) {
for(c=24; c<=100; c++) {
for(d=24; d<=100; d++) {
if( (a+b) >= 73 ) {
if( (a+b) <= 121 ) {
if( (c+d) >= 90 ) {
if( (c+d) <= 134) {
double n = (a/(double)b) * (c/(double)d);
fprintf("Found answer : a=%d, b=%d, c=%d, d=%d, n=%f\n", a, b, c, d, n);
}
}
}
}
}
}
}
}
}