| Total Complexity | 16 |
| Total Lines | 54 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 7 | class DivideTwoIntegers |
||
| 8 | { |
||
| 9 | public static function divide(int $x, int $y): int |
||
| 10 | { |
||
| 11 | [$max, $min] = [2147483647, -2147483648]; |
||
| 12 | if ($x === $y) { |
||
| 13 | return 1; |
||
| 14 | } |
||
| 15 | if ($y === 1) { |
||
| 16 | return $x; |
||
| 17 | } |
||
| 18 | if ($x === 0) { |
||
| 19 | return 0; |
||
| 20 | } |
||
| 21 | if ($x === $min && $y === -1) { |
||
| 22 | return $max; |
||
| 23 | } |
||
| 24 | |||
| 25 | $sign = $x > 0 ^ $y > 0 ? -1 : 1; |
||
| 26 | [$n, $x, $y] = [0, abs($x), abs($y)]; |
||
| 27 | while ($x >= $y) { |
||
| 28 | [$t, $i] = [$y, 1]; |
||
| 29 | while ($x >= $t) { |
||
| 30 | $x -= $t; |
||
| 31 | $n += $i; |
||
| 32 | $i <<= 1; |
||
| 33 | $t <<= 1; |
||
| 34 | } |
||
| 35 | } |
||
| 36 | if ($sign < 0) { |
||
| 37 | $n = -$n; |
||
| 38 | } |
||
| 39 | |||
| 40 | return min(max($min, $n), $max); |
||
| 41 | } |
||
| 42 | |||
| 43 | public static function divide2(int $x, int $y): int |
||
| 61 | } |
||
| 62 | } |
||
| 63 |