Search code examples
pythonpuzzle

How many small squares needed to fill a rectangle?


I am trying to solve a problem on codeforces. I am a beginner and I am just trying to understand how to solve this problem I do not want to copy someone's code.

Requirements are :

There is 1 big rectangle of n * m units. (n and m are length and width) and small squares of length a are suppose to fill it up. Its okay to fill a little more are if a squares don't cover the big square (n*m) completely.

If n = 6 and m = 6. area of n*m becomes 36 units. and suppose a = 4 then each tile covers 16 units of area.

Now when we lay down small squares on big rectangle it comes to 4 small squares needed to cover big rectangle.

I can easily calculate the area of large rectangle:

l_area = n * m

area of small squares :

s_area = a*a

Now how to calculate the number of small squares needed to cover the big rectangle.

The divide operator does not work for obvious reason.


Solution

  • Just compute how many squares you need to fill up n, and how many to fill out m, and multiply. In your example, the small square is length 4, so you need 2 for m and 2 for n, so you need 2x2 = 4 squares.

    To get the number of squares for one side, you divide and round up. One way to do this is

    math.ceil(float(bigLen)/float(smallLen)))