Skip to content

Instantly share code, notes, and snippets.

@peteroome
Last active February 5, 2016 08:29
Show Gist options
  • Save peteroome/d8e85ef3f800ed682494 to your computer and use it in GitHub Desktop.
Save peteroome/d8e85ef3f800ed682494 to your computer and use it in GitHub Desktop.
Algorithm to get the maximum size of n squares that fit into a rectangle with a given width and height (http://math.stackexchange.com/questions/466198/algorithm-to-get-the-maximum-size-of-n-squares-that-fit-into-a-rectangle-with-a) #WIP
int main(){
double x=5, y=6, n=7;//values here
double px=ceil(sqrt(n*x/y));
double sx,sy;
if(floor(px*y/x)*px<n) //does not fit, y/(x/px)=px*y/x
sx=y/ceil(px*y/x);
else
sx= x/px;
double py=ceil(sqrt(n*y/x));
if(floor(py*x/y)*py<n) //does not fit
sy=x/ceil(x*py/y);
else
sy=y/py;
printf("%f",MAX(sx,sy));
return 0;
}
def values(x = 5.0, y = 6.0, a = 7.0)
n = (a / 4.0).ceil
px = ( Math.sqrt( n * x / y) ).to_f.ceil
sx = 0.0
sy = 0.0
if (px * y / x).floor * px < n # does not fit, y/(x/px)=px*y/x
sx = y / (px * y / x).to_f.ceil
else
sx = x / px
end
py = ( Math.sqrt(n * y / x) ).to_f.ceil
if (py * x / y).floor * py < n # does not fit
sy = x / (x * py / y).to_f.ceil
else
sy = y / py
end
# printf "%f", [sx, sy].max
v = [sx.round(2), sy.round(2)].max
printf "%f", v
return v
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment