# Finding formula?

Abu Nene

Ranch Hand

Posts: 56

posted 5 years ago

- 0

Hi guys, in the Java application that I'm working with, I need to create a method which takes in 3 int parameters, calculate and return a int. Problem is I need to find out the formula. I've some input data and return result data as follows:

calculate(3, 4, 4) return 0

calculate(1, 5, 5) return 0

calculate(2, 4, 2) return -2

calculate(0, 5, 3) return -2

calculate(4, 6, 4) return -2

calculate(0, 3, 5) return 1

Any idea what is the best way to find out the formula? Do pardon me if this sound more like a math problem rather then Java. Please advise thanks.

calculate(3, 4, 4) return 0

calculate(1, 5, 5) return 0

calculate(2, 4, 2) return -2

calculate(0, 5, 3) return -2

calculate(4, 6, 4) return -2

calculate(0, 3, 5) return 1

Any idea what is the best way to find out the formula? Do pardon me if this sound more like a math problem rather then Java. Please advise thanks.

Ulf Dittmer

Rancher

Posts: 42966

73

posted 5 years ago

- 0

I would contact the author because there are a million and one solutions. Are you sure that you don't have the necessary code/documentation?

Another option would be decompiling with javap -c className

Another option would be decompiling with javap -c className

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler

Please correct my English.

bhaskar ramachandra reddy

Greenhorn

Posts: 2

Abu Nene

Ranch Hand

Posts: 56

posted 5 years ago

- 0

The logic have not been created. How I get the data currently is getting the 3rd parameter minus the 2nd parameter but it'll not satisfy "calculate(0, 3, 5) return 1".

The logic is a mathematical formula which satisfy the all given inputs together with the output. Is there something like a reserve math or something?

The logic is a mathematical formula which satisfy the all given inputs together with the output. Is there something like a reserve math or something?

Ulf Dittmer

Rancher

Posts: 42966

73

posted 5 years ago

In the field of statistics, I believe this is done via regression analysis -- although admittedly, it is done as "best fit", and not exact. In the field of graphics, there is "curve fitting", which may be able to be applied.

Maybe a google of "regression analysis" and / or "curve fitting" can get you started.

Henry

- 0

In the field of statistics, I believe this is done via regression analysis -- although admittedly, it is done as "best fit", and not exact. In the field of graphics, there is "curve fitting", which may be able to be applied.

Maybe a google of "regression analysis" and / or "curve fitting" can get you started.

Henry

posted 5 years ago

- 0

you could define an infinite number of formulas that 'solve' the 6 data points you have. As EFH said, a simple table would work:

if (input = 3,4,4) return 0

else if (input = 1,5,5) return 0

else if (input = 2,4,3) return -2

else if (input = 0,5,3) return -2

else if (input = 4,6,4) return -2

else if (input = 0,3,5) return 1

//at this point, I could add 0 to 10,000 more "else if" lines defining any 3-digit combination I want

//and an optional 'else' line

If you're looking for a mathematical formula, again, you have 6 data points in a 3-d space. If you are not limited to some kind of surface/shape, there are an infinite number of solutions.

if (input = 3,4,4) return 0

else if (input = 1,5,5) return 0

else if (input = 2,4,3) return -2

else if (input = 0,5,3) return -2

else if (input = 4,6,4) return -2

else if (input = 0,3,5) return 1

//at this point, I could add 0 to 10,000 more "else if" lines defining any 3-digit combination I want

//and an optional 'else' line

If you're looking for a mathematical formula, again, you have 6 data points in a 3-d space. If you are not limited to some kind of surface/shape, there are an infinite number of solutions.

posted 5 years ago

- 0

If you are only using addition and multiplication, and no if-statements, you get a lot of different variables in your function:

3 * a + 4 * b + 4 * c + d == 0 (1)

1 * a + 5 * b + 5 * c + d == 0 (2)

2 * a + 4 * b + 2 * c + d == -2 (3)

0 * a + 5 * b + 3 * c + d == -2 (4)

4 * a + 6 * b + 4 * c + d == -2 (5)

0 * a + 3 * b + 5 * c + d -- 1 (6)

By combining all these you can get the solution for a, b, c and d. For instance, combining (1) and (2):

3 * a + 4 * b + 4 * c + d == 1 * a + 5 * b + 5 * c + d

=== {subtract 1 * a, 4 * b, 4 * c and d on each side}

2 * a == b + c (7)

You can then combine (3) and (7), by replacing the 2 * a with b + c in (3):

b + c + 4 * b + 2 * c + d == -2

===

5 * b + 3 * c + d == -2 (8)

If you have enough statements you can eventually work out the values of a, b, c and d. Note that the more variables you have, the more statements you need. 6 may not be enough for 4 variables.

3 * a + 4 * b + 4 * c + d == 0 (1)

1 * a + 5 * b + 5 * c + d == 0 (2)

2 * a + 4 * b + 2 * c + d == -2 (3)

0 * a + 5 * b + 3 * c + d == -2 (4)

4 * a + 6 * b + 4 * c + d == -2 (5)

0 * a + 3 * b + 5 * c + d -- 1 (6)

By combining all these you can get the solution for a, b, c and d. For instance, combining (1) and (2):

3 * a + 4 * b + 4 * c + d == 1 * a + 5 * b + 5 * c + d

=== {subtract 1 * a, 4 * b, 4 * c and d on each side}

2 * a == b + c (7)

You can then combine (3) and (7), by replacing the 2 * a with b + c in (3):

b + c + 4 * b + 2 * c + d == -2

===

5 * b + 3 * c + d == -2 (8)

If you have enough statements you can eventually work out the values of a, b, c and d. Note that the more variables you have, the more statements you need. 6 may not be enough for 4 variables.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

posted 5 years ago

- 0

Ok, I've worked it out, and unless I've made a mistake then there are no a, b, c and d that match these equations. Consider:

c == 1 and c == 1/2 so there is contradiction. If the result of (6) was 2 instead of 1 then a == 0, b == -1, c == -1 and d == 0 would be the solution.

Of course this doesn't prevent you using more complex functions like using powers of any of the numbers. All I've shown is that a simple linear function is not possible.

c == 1 and c == 1/2 so there is contradiction. If the result of (6) was 2 instead of 1 then a == 0, b == -1, c == -1 and d == 0 would be the solution.

Of course this doesn't prevent you using more complex functions like using powers of any of the numbers. All I've shown is that a simple linear function is not possible.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

posted 5 years ago

- 0

If you're talking about straight algebra, you need one formula per variable, so 6 is more than enough. in fact, here three are too many. Three would do it (the three inputs are the co-efficients of x, y and z in

ax + by + cz = <return value>

ax + by + cz = <return value>

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Sha Jar

Greenhorn

Posts: 23

posted 5 years ago

Wouldn't you need 4, with the "+ d" I specified as correction for the return value?

- 0

fred rosenberger wrote:If you're talking about straight algebra, you need one formula per variable, so 6 is more than enough. in fact, here three are too many. Three would do it (the three inputs are the co-efficients of x, y and z in

ax + by + cz = <return value>

Wouldn't you need 4, with the "+ d" I specified as correction for the return value?

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions

It is sorta covered in the JavaRanch Style Guide. |