# A Megachess Board

## JavaScript (ES7), 30 bytes

```
b=>w=>((b<w?b:w)*2|b!=w)**.5|0
```

Try it online!

Given the number of black squares \$b\$ and the number of white squares \$w\$, this computes:

$$s=\left\lfloor\sqrt{2\times\min(b,w)+k}\right\rfloor$$ with: $$k=\begin{cases} 0&\text{if }b=w\\ 1&\text{if }b\neq w \end{cases}$$

For even sizes \$s\$, we need \$s^2/2\$ squares of each kind (e.g. for \$s=8\$: \$32\$ black squares and \$32\$ white squares).

For odd sizes \$s\$, we need \$\lfloor s^2/2\rfloor\$ squares of one kind and \$\lceil s^2/2\rceil\$ squares of the other kind (e.g. for \$s=5\$: \$12\$ black squares and \$13\$ white squares, or the other way around). The parameter \$k\$ is set to \$1\$ if \$max(b,w)\geq min(b,w)+1\$, which represents this extra square on one side.

## Jelly, 6 bytes

```
«Ḥ+nÆ½
```

Try it online!

A dyadic link that returns the maximum board size.

Test suite for all permutations of numbers to 20

### Explanation

```
« | Minimum
Ḥ | Doubled
+n | Plus 1 if inputs not equal
Æ½ | Integer square root
```

Arnauld’s answer has a good explanation for *why* this works; please consider upvoting him too!

## Haskell, 35 bytes

```
x#y=floor$sqrt$min(x+y)$1+2*min x y
```

Try it online!

### Explanation

This answer calculates the following formula:

$$\left\lfloor\sqrt{\min(a+b,2\min(a,b)+1)}\right\rfloor$$

Why does this formula work? Well lets start by noting the following:

*Every square of even side length can be tiled by \$2\times 1\$ tiles.*

and

*Every square of odd length can be tiled, spare a single \$1\times 1\$ square, by \$2\times 1\$ tiles.*

Now we note that if we put these \$2\times 1\$ tiles on a chessboard each would lay on top of one black square and on white square. So if we make an even chessboard every tile needs to have a pair of the other color, and if we make an odd chessboard every tile but one needs a pair of the other color. This tells us that the answer is never more than \$\left\lfloor\sqrt{2\min(a,b)+1}\right\rfloor\$. \$2\min(a,b)\$ is the maximum number of pairs we can make and the \$+1\$ is for the last square that doesnt' need a pair. The problem with this is that if \$a=b\$ we will not have the extra square for the odd case. So we add another condition: Our result cannot be more than \$\left\lfloor\sqrt{a+b}\right\rfloor\$. That is we can't make a square which has more tiles than we have available.

So we just take the lesser of the two options.

$$\left\lfloor\sqrt{\min(a+b,2\min(a,b)+1)}\right\rfloor$$

We can notice that this is the same as Arnauld's formulation since if \$a=b\$ then \$2\min(a,b)\$ is just \$a+b\$.