Integer division that rounds up: Difference between revisions
(→Proof) |
(→Proof) |
||
| Line 41: | Line 41: | ||
&= | &= | ||
\left \lfloor \frac{by + y}{y} + \frac {r-1}y \right \rfloor \\ | \left \lfloor \frac{by + y}{y} + \frac {r-1}y \right \rfloor \\ | ||
&= | |||
\left \lfloor \frac{(1+y)b}{y} + \frac {r-1}y \right \rfloor \\ | |||
\end{align} | \end{align} | ||
</math> | </math> | ||
Revision as of 10:18, 8 July 2024
Introduction
Usual integer division rounds down: for Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle a,b \in \mathbb N, b\neq 0} . To round up (if overflow is not an issue), you can use following algorithm with the usual roundig down division: Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle q = \frac{x+y-1}{y} = \left \lceil \frac xy \right \rceil }
Proof
Proof is in two parts; 1st if Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle y} divides Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x} , and if not. Note that usual integer division rounds down.
Part 1. If Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle y} divides Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x} we have Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x=ay} for some Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle a\in\mathbb N_+} . Thus we have
Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \begin{align} \left \lfloor \frac{x+y-1}{y} \right \rfloor &= \left \lfloor \frac{x}{y} + \frac{y-1}y \right \rfloor \\ &= \left \lfloor \frac{ay}{y} + \frac{y-1}y \right \rfloor \\ &= \frac{ay}{y} \\ &= \frac{x}{y} \end{align} }
because Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle 0 \leq \frac{y-1}y < 1} . This part is ok.
Part 2. If Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle y} does not divide Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x} we have Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle x=by + r} for some Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle b\in\mathbb N_+} and Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle 0<r<b} . Thus we have
Failed to parse (SVG (MathML can be enabled via browser plugin): Invalid response ("Math extension cannot connect to Restbase.") from server "https://wikimedia.org/api/rest_v1/":): {\displaystyle \begin{align} \left \lfloor \frac{x+y-1}{y} \right \rfloor &= \left \lfloor \frac{x}{y} + \frac{y-1}y \right \rfloor \\ &= \left \lfloor \frac{by+r}{y} + \frac{y-1}y \right \rfloor \\ &= \left \lfloor \frac{by}{y} + \frac ry + \frac yy - \frac 1y \right \rfloor \\ &= \left \lfloor \frac{by + y}{y} + \frac {r-1}y \right \rfloor \\ &= \left \lfloor \frac{(1+y)b}{y} + \frac {r-1}y \right \rfloor \\ \end{align} }