11
Background
I have a bunch of square-shaped boxes of equal size, and since I'm a neat person, I want to arrange them all into a square formation. However, their number is not necessarily a perfect square, so I may have to approximate the square shape. I want you to find me the most aesthetically pleasing arrangement -- programmatically, of course.
Input
Your input is a single positive integer k, representing the number of boxes.
Output
Your program shall choose two positive integers m, n such that m*(n-1) < k ≤ m*n holds.
They represent the width and height of the large square-like shape we are arranging.
Since we are looking for aestethically pleasing shapes, the quantity (m - n)2 + (m*n - k)2 shall be minimal, so that the shape is close to a square, and its area is close to k.
If there are still several candidates for the pair (m, n), choose the one where the width m is maximal.
Now, your actual output shall not be the numbers m and n.
Instead, you shall print the arrangement of boxes, using the character # to represent a box.
More specifically, you shall print n-1 rows, each of which consists of m characters #, and then one row of k - m*(n-1) characters #.
Note that the output contains exactly k characters #.
Rules and Scoring
There shall not be any leading or trailing whitespace in the output, except that the last row may be padded with trailing spaces to be of length m, if desired.
There may be one trailing newline, but no preceding newlines.
You may use any printable ASCII character in place of #, if desired.
You may write a full program, or return a string from a function. The lowest byte count wins, and standard loopholes are disallowed.
Test Cases
Here are the correct outputs for a few input values.
1
#
2
##
3
##
#
4
##
##
8
###
###
##
13
#####
#####
###
17
######
######
#####
18
#####
#####
#####
###
20
#####
#####
#####
#####
21
######
######
######
###
22
######
######
######
####
23
#####
#####
#####
#####
###
I actually count 231 bytes in your source, not 234. But regardless, you can reduce it by decreasing your indent size from four spaces to one space. It'll work the same. – Alex A. – 2015-08-25T04:59:55.657
This is a handy tool for getting your byte count. By the way, nice submission and welcome to the site! – Alex A. – 2015-08-25T05:12:45.327
:missing at line 5. Comma is what defines a tuple, brackets()can be removed at line 6. Spaces between)and (iforfor) too.maxcan get generator as parameter, thus brackets[]are redundant. You iterate overdkeys, so you can safely used[i]. – Trang Oul – 2015-08-25T11:06:05.160You can save two bytes changing
(i+1)*xto-~i*xori*x+x. – Kade – 2015-08-25T13:23:39.180You have an extra, invalid paren at
(i*x+x... – FlipTack – 2016-12-21T19:35:43.707