- https://www.codewars.com/kata/565c9f5fcabeeaeb5a000052 Area of a regular polygon.
Started at 21:10 2020.09.17 Thursday Sep.
Finished at 23:02 2020.09.17 Thursday Sep. (1hr 58minutes)
<?php | |
# https://www.codewars.com/kata/565c9f5fcabeeaeb5a000052 Area of a regular polygon. | |
function area_of_regular_polygon(string $s): string { | |
$not = 'Invalid input'; | |
echo "Input: $s\n"; | |
if( ! preg_match('/(\d+.?\d*) sides of (\d+.?\d*) (\w+) each/',$s,$match)) return $not; | |
$n = (float) $match[1]; (float) $side = $match[2]; $units = $match[3]; | |
echo "$n - sides of $side length \n"; | |
if(2 >= $n || 0 >= $side || 0 != $n - floor($n)) return $not; | |
$a = ($side*$side)*$n; | |
$b = 4*tan(pi()/$n); | |
return sprintf("%.2f sq.$units", round($a/$b,2)); | |
} |
Started at 21:10 2020.09.17 Thursday Sep.
Finished at 23:02 2020.09.17 Thursday Sep. (1hr 58minutes)