Last active
May 23, 2018 14:50
-
-
Save tvolf/5f4835886ac71e4df31b444e125f1fdd to your computer and use it in GitHub Desktop.
Solution for task #96 on @unilecs telegram channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
используем свойство Сrossed Ladders Theorem: | |
1/A + 1/B = 1/h | |
где A = sqrt(n^2 + w^2), | |
B = sqrt(m^2 + w^2) | |
Далее просто используем итерационный процесс для формулы | |
1/sqrt(n^2 + w^2) + 1/sqrt(m^2 + w^2) - 1/h | |
и ищем такое значение w на интервале от 0 до 1000, | |
для которого значение формулы будет максимально близко к нулю. | |
*/ | |
function task96($n, $m, $h) { | |
$minDiff = 1e20; | |
$result = -1; | |
for ($w = 0; $w <= 1000; $w += 0.01) { | |
$v = 1/sqrt($n * $n - $w * $w) + 1/sqrt($m * $m - $w * $w) - 1/$h; | |
if (abs($v) < $minDiff) { | |
$minDiff = abs($v); | |
$result = $w; | |
} | |
} | |
return $result === -1 ? 'Incorrect input data' : sprintf("%0.2f", $result); | |
} | |
function test($n, $m, $h) { | |
printf("%d, %d, %d => %s\n", $n, $m, $h, task96($n, $m, $h)); | |
} | |
test(40, 30, 10); // 26.03 | |
test(1000, 1000, 353.55); // 707.11 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment