Skip to content

Instantly share code, notes, and snippets.

@shinyzhu
Created August 19, 2010 03:08
Show Gist options
  • Save shinyzhu/536897 to your computer and use it in GitHub Desktop.
Save shinyzhu/536897 to your computer and use it in GitHub Desktop.
<?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