Created
August 19, 2010 03:08
-
-
Save shinyzhu/536897 to your computer and use it in GitHub Desktop.
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 | |
#Solution for calculating xy=total, x-y->0. | |
#y is always equal or greater than x | |
#Reference: http://www.wolframalpha.com/input/?i=xy%3D200,+x%3E0 | |
function min_xy($total){ | |
$all_xy = array(); | |
$min = $total;//minimum number | |
$min_idx = -1;//minium number position in $all_xy | |
for($x = 1; $x <= $total; $x++){ | |
if(($total % $x == 0) && ($x <= ($total / $x))){ | |
$all_xy[] = array($x, ($total / $x)); | |
} | |
} | |
foreach($all_xy as $i => $xy){ | |
if(($xy[1] - $xy[0]) < $min){ | |
$min = $xy[1] - $xy[0]; | |
$min_idx = $i; | |
} | |
} | |
return $all_xy[$min_idx]; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment