Find largest $a, b \in \mathbb{N}$ so that $a \times b \geq n$

55 Views Asked by At

I have the following problem: I have $n$ images that I want to display on-screen. $n$ might be any number from $\mathbb{N}$. If I have one Image, I want this layout (* is an image, _ is empty space):

*

If I have two images, I want to have it like this:

**

For three images, I want

**
*_

For four images, I want

**
**

and so on. So, for aligning this, I need a function that returns two values, the height and the width (1 = one image).

Let's say I have this function, it's output should look like this:

$ f(n) = (x, y)$

$ f(1) = (1, 1)$

$ f(2) = (1, 2)$

$ f(3) = f(4) = (2, 2)$

$ f(5) = f(6) = (3, 2) $

$ f(7) = f(8) = (2, 4) $

$ f(9) = (3, 3) $

$ f(10) = (5, 2) $

$ f(11) = f(12) = (4, 3) $

The $x$-$y$-order does not really matter that much here.

My problem here is that I don't know how to non-statically write such a function. Stating it for, like, the first 100 numbers isn't gonna cut it, because n might be very large here.

As far as I see, for non-prime-numbers I get the two largest divisors and for prime numbers, I add some $n$ up to the point where it isn't a prime number anymore and then go to step one (the largest divisors).

But is there any elegant way of doing this?