The old roman army formations are very famous around the world. In these formations roman legionaries grouped in a geometric shape (usually a rectangle) protecting the flanks and the superior part of it using their shields. The legionaries at interior positions covered the superior part placing their shield above their heads, the legionaries at the flanks carried 2 or more shields: one for protecting the superior part, and one or more shields for protecting the flanks (if someone was in the corner he had 3 shields, if someone was alone in a formation he had 5 shields Yes, I know it is impossible for a human to carry 5 shields, but somehow they did it). Using this formation all roman legionaries protected themselves and were the hardest opponent at the time.
The history tells there was a roman general who stated that the best formation shape was the square (same number of legionaries in rows and columns). The problem was figuring out how many formations (and the size) he should split his army in order to:
- Do not left any legionary out of a formation (although he admitted single legionary formation)
- Reduce the amount of required shields
The general, after doing some math and calculations, he figured out that the best way to accomplish this 2 conditions is to start with the biggest square possible, and then repeat until no legionaries left.
Example:
If 35 legionaries in his army, the formation consisted in
- A 5x5 legionaries square (This is the biggest square possible).
With the remaining legionaries (10)
- A 3x3 square
With the remaining legionaries (1)
- A 1x1 square.
At the end it will look something like this:
5x5
* * * * * 3x3
* * * * * * * * 1x1
* * * * * * * * *
* * * * * * * *
* * * * *
The legionaries at interior positions covered the superior part placing their shield above their heads. They only needed 1 shield.
* * * * *
* 1 1 1 * * * *
* 1 1 1 * * 1 * *
* 1 1 1 * * * *
* * * * *
The legionaries at the flanks carried 2
* 2 2 2 *
2 1 1 1 2 * 2 *
2 1 1 1 2 2 1 2 *
2 1 1 1 2 * 2 *
* 2 2 2 *
If someone was in the corner he had 3 shields
3 2 2 2 3
2 1 1 1 2 3 2 3
2 1 1 1 2 2 1 2 *
2 1 1 1 2 3 2 3
3 2 2 2 3
If someone was alone in a formation he had 5 shields
3 2 2 2 3
2 1 1 1 2 3 2 3
2 1 1 1 2 2 1 2 5
2 1 1 1 2 3 2 3
3 2 2 2 3
This formation required a total of 71 shields.
Challenge
- Calculate the amount of shields that are needed for a X amount of legionaries
Input
- Amount of legionaries in the army
Output
- Amount of shields needed.
Test Cases
35 => 71
20 => 44
10 => 26
32 => 72
- Standard code-golf rules apply
Amazon.com : Best-selling Nipple Shield Carrying Case, Perfect...
so I guess I'll never truly know. Did they actually carry 5 shields-- or was this to make the question work :P? \$\endgroup\$