Inverting the equality which contains the operation of taking integer part

60 Views Asked by At

I was recently presented with the following equality $$ n = \left[\frac{w}{2d+a}\right]\cdot \left[\frac{h}{2d+b}\right] $$ where all participating variables are non-negative integers, and $[\ldots]$ is the operation of taking integer part (e.g $[6.789]=6$). The problem is to express $d$, i.e. solve this equality for $d$, and get a solution in form of $f(a,b,w,h,n)\leq d\leq F(a,b,w,h,n)$; or some simular way that would allow to determine the range of values of $d$ by given values of $a,b,w,h,n$ in constant time, algorithmically speaking.

Sure enough, if it wasn't for these square brackets, the problem would be a piece of cake: $$ n = \frac{w}{2d+a}\cdot \frac{h}{2d+b} = \frac{wh}{(2d+a)(2d+b)}=\frac{wh}{4d^2+2d(a+b)+ab} $$ $$ 4d^2+2d(a+b)+ab-\frac{wh}{n} = 0 $$ $$ d = -\frac{1}{4}(a+b)\pm\frac{1}{4}\sqrt{(a+b)^2-4ab+\frac{4hw}{n}} $$ But unfortunately $[x][y]\neq [xy]$. Is there any way to deal with the integer operation?

P.S. In the original problem it would suffice to find the largest such $d$.


Following suggestion in David Kleiman's answer, let's have $\left[\frac{w}{2d+a}\right] = \frac{w}{2d+a} - r_1$ and $\left[\frac{h}{2d+b}\right] = \frac{h}{2d+b} - r_2$, where $r_i\in [0, 1)$. Also let $q = 2d$ for convenience. Now the equality takes form: $$ n = \left(\frac{w}{q+a} - r_1\right)\cdot \left(\frac{h}{q+b} - r_2\right) $$ Now let's bastardize this beautiful formula big time by removing parentheses, liquidating fractions and gathering coefficents of the powers of $q$:

$$ q^2(n-r_1r_2)+q((n-r_1r_2)(a+b)+r_1h+r_2w)+abn-wh+r_1ha+r_2wb-r_1r_2ab=0 $$

It seemed covenient to introduce denotations $m=n-r_1r_2$ and $c=a+b$, so:

$$ q^2m+q(mc+r_1h+r_2w)+abn-wh+r_1ha+r_2wb-r_1r_2ab=0 $$

Solving the quadric equation for $q$ and choosing positive sign we get: $$ q = \frac{-(mc+r_1h+r_2w)+\sqrt{D}}{2m}, $$ where $$ D = (mc+r_1h+r_2w)^2-4m(abn-wh+r_1ha+r_2wb-r_1r_2ab). $$ Putting all this together we have: $$ q = \frac{-((n-r_1r_2)(a+b)+r_1h+r_2w)}{2(n-r_1r_2)}+\frac{\sqrt{((n-r_1r_2)(a+b)+r_1h+r_2w)^2-4(n-r_1r_2)(abn-wh+r_1ha+r_2wb-r_1r_2ab)}}{2(n-r_1r_2)} $$ So... how to maximize it?

1

There are 1 best solutions below

2
On

I'm not sure if this is what you're looking for, but you can write $[x] = x - r$ where $0 \leq r < 1$. You can do this for both factors (with $r_1$ and $r_2$ say), and then solve for $d$ using the quadratic formula. Then you can maximize (minimize) $d$ as a function of $r_1$ and $r_2$.

This will likely be a very tedious calculation, but since you have a bounded domain, the function will attain its max and min.