Question about making a custom 'formula'

60 Views Asked by At

I am working on some program for myself, and I am stuck on one thing, which I am not sure how to make exactly, I do know how to calculate it manually on paper, but I don't know any kind of formula or algorithm to help me make a program do it.

So, basically, this is what I'm trying to do.

I have 3 digits (floating point numbers). Examples: 1.25, 5.20, 9.00, 18.00, 2.95, 2.05 etc.

Now, I want to give a real example of what I'm doing manually:

I have 3 digits as we said, so example this:

2.35 3.15 2.45

And, I do multiplication of each number with 'specific' number, so I can check whether the number after multiplication is lower than the total sum of the multiplicands.

Example:

2.35 * 1.03 = 2.4205

3.15 * 0.97 = 3.0555

2.45 * 1 = 2.45

In this case, my program should give a warning that the 3 numbers do not meet the requirements, because:

1.03 + 0.97 + 1 = 3, and 2.4205 < 3, and also 2.45 < 3, so only 2nd one meets the condition. (all should meet the condition to be correct)

Here is an example, where it is correct:

The 3 numbers are: 1.25, 9.00 and 13.0

1.25 * 5.2 = 6.5

9.00 * 0.8 = 7.2

13.0 * 0.5 = 6.5

So, it is correct because, all of the results after multiplication are either bigger or equal to the total sum of all multiplicands. In this case, 5.2 + 0.8 + 0.5 = 6.5. And that means, 6.5 >= 6.5, 7.2 >= 6.5, 6.5 >= 6.5.

So, I was wondering, is there any other way I could use or some algorithm that I could use in my program to do this calculation? I have to do lots of calculations daily, so I want to make my job easier with a program.

Thanks in advance!

1

There are 1 best solutions below

5
On BEST ANSWER

Let $a_1,a_2,a_3$ be positive real numbers. We want to find positve real numbers $b_1, b_2, b_3$ such that $a_ib_i\ge b_1+b_2+b_3$ for $i=1,2,3$. We may multiply all $b_i$ with a positive constant without any changes to the conditions, hence assume wlog. that $b_1+b_2+b_3=1$. Then we obtain the condition $b_i\ge \frac1{a_i}$. This is compatible with $b_1+b_2+b_3=1$ if and only if $\frac1{a_1}+\frac1{a_2}+\frac1{a_3}\le 1$. Indeed, if $\frac1{a_1}+\frac1{a_2}+\frac1{a_3}\le 1$, we can let $b_1=\frac1{a_1}$, $b_2=\frac1{a_2}$, $b_3=1-b_1-b_2$ and are done.

Example: with $a_1 = 1.25$, $a_2= 9$, $a_3= 13$, we have $\frac1{a_1}+\frac1{a_2}+\frac1{a_3}\approx 0.988<1$, so all is fine and we have a decent amount of "free space". We can let $b_1=\frac1{a_1}=0.8$, and $b_2=0.12$ (which is a generous round-up of $\frac1{a_2}=0.1111\ldots$) and $b_3=1-b_1-b_2=0.08$ (which, luckily, is greater than $\frac1{a_3}=0.076923\ldots$). Then $b_1+b_2+b_3=1$, $a_1b_1=1\ge 1$, $a_2b_2= 1.08\ge 1$, $a_3b_3=1.04\ge 1$.