How to stack fruits in a neat pile

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); // Round it up to nearest whole number
}
+4
source share
1 answer

The number of fruits in a pyramid with height n is given by the nth triangular number given by the equation

T n= n (n + 1)/2

, 2 2 (2 + 1)/2 = 3 . 4 4 (4 + 1)/2 = 10 .

k , , n, T n & ge; . :

T n= k

n (n + 1)/2 = k

n 2 + n = 2k

n 2 + n - 2k = 0

,

n = (-1 & plusmn; & radic; (1 + 8k))/2

, n

n = (& radic; (8k + 1) - 1)/2

, .

. , 9 . ,

n = (& radic; (72 + 1) - 1)/2 = (& radic; (73) - 1)/2 = 3,772001873

k = 4, 4.

, 137 . n = 16.060495162, 17.

, !

+4

Source: https://habr.com/ru/post/1570938/


All Articles