Calculate the maximum number of circles of radius r that can fit in a rectangle with width x and height y. Write a function that take r, x and y as input and returns the number of circles that fit in the rectangle.


  • 12
    \$\begingroup\$ I would suggest to add some testcases so that we can know if any answer is correct. \$\endgroup\$
    – tsh
    Nov 3, 2018 at 15:44
  • 5
    \$\begingroup\$ It appears to me that one could fit at least one more circle, by expanding the second-to-last row to have 10 circles in it, such that the last three rows touch each other orthogonally. Do we not have to handle such cases? \$\endgroup\$ Nov 3, 2018 at 15:56
  • 5
    \$\begingroup\$ Would it make sense to just normalise and say r is always 1? This way there are only 2 inputs - I believe every answer will divide through by r. \$\endgroup\$ Nov 3, 2018 at 15:58
  • 2
    \$\begingroup\$ Do we have to support floats or are x,y,r guaranteed to be whole numbers? \$\endgroup\$
    – Kateba
    Nov 3, 2018 at 16:34
  • 2
    \$\begingroup\$ Without the "triangular lattice" restriction we have math.stackexchange.com/questions/701/… , which is unsolved. \$\endgroup\$
    Nov 3, 2018 at 16:45


Browse other questions tagged or ask your own question.