I chose Java for this case, because the language is simple enough for translation.
What will be the mathematical algorithm for determining the number of fruits needed on the bottom line to collect the X number of fruits in a template like this? (ignoring the force 2 that I am squaring)
* 1
* 2 3 = 2
*
* 1 2
* 3 4 5 = 3
*
* 1
* 2 3
* 4 5 6 = 3
*
* 1 2 3
* 4 5 6 7 = 4
*
* 1 2
* 3 4 5
* 6 7 8 9 = 4
*
* 1
* 2 3
* 4 5 6
* 7 8 9 X = 4
*
* 1 2 3
* 3 4 5 6
* 7 8 9 X 1 = 5
Initially, I thought it would be easy, but as the numbers get higher, I begin to think that this is more of a factorial.
Edit: adding code translated with the answer below, @templatetypepedef
private int _getBottomLineCount() {
double insideSquareRoot = (8 * numberOfApples) +1;
double squareRoot = Math.sqrt(insideSquareRoot);
double val = (squareRoot -1) /2;
return (int) Math.ceil(val);
}
source
share