Statistics

Problem Statement for "CannonBalls"

Problem Statement

Captain Brown Beard keeps his pirate ship stocked with a healthy supply of cannon balls. Because he is very tidy, he insists that all of the cannon balls be stacked into perfect tetrahedron shapes. Such a pyramid is constructed by arranging cannon balls into an equilateral triangle with side length n. Then, on top of that is stacked an equilateral triangle of side length n-1, and so on, until there is a single cannon ball placed on the top (this single cannon ball is a triangle of side length 1). For example, a stack of size 3 will have three layers that look like this (from top to bottom):

  X

  X
 X X

  X
 X X
X X X

So, each triangle will contain 1, 3, 6, 10, etc. cannon balls. Thus, any complete stack will have 1, 4, 10, 20, etc. cannon balls.

You are given an int n, the number of cannon balls loaded on the ship. You are to return an int indicating the least possible number of stacks into which the cannon balls can be piled.

Definition

Class:
CannonBalls
Method:
fewestPiles
Parameters:
int
Returns:
int
Method signature:
int fewestPiles(int n)
(be sure your method is public)

Constraints

  • n will be between 1 and 300000, inclusive.

Examples

  1. 1

    Returns: 1

    This is the smallest single stack we can make.

  2. 5

    Returns: 2

    A stack with 1 cannon ball, and a stack with 4 cannon balls.

  3. 9

    Returns: 3

    9 = 4 + 4 + 1

  4. 15

    Returns: 3

  5. 40

    Returns: 2

  6. 91

    Returns: 2

    91 = 56 + 35

  7. 68296

    Returns: 3

  8. 248308

    Returns: 4

  9. 125494

    Returns: 4

  10. 79764

    Returns: 3

  11. 246972

    Returns: 4

  12. 92798

    Returns: 4

  13. 80472

    Returns: 3

  14. 79490

    Returns: 4

  15. 223820

    Returns: 4

  16. 84176

    Returns: 4

  17. 110567

    Returns: 4

  18. 146520

    Returns: 3

  19. 280787

    Returns: 4

  20. 98910

    Returns: 3

  21. 41829

    Returns: 2

  22. 90581

    Returns: 4

  23. 80548

    Returns: 4

  24. 95725

    Returns: 4

  25. 137134

    Returns: 3

  26. 129416

    Returns: 3

  27. 244306

    Returns: 3

  28. 56192

    Returns: 4

  29. 80968

    Returns: 4

  30. 90204

    Returns: 3

  31. 8059

    Returns: 3

  32. 290687

    Returns: 4

  33. 43576

    Returns: 4

  34. 36753

    Returns: 4

  35. 61262

    Returns: 3

  36. 285975

    Returns: 3

  37. 300000

    Returns: 3

  38. 200000

    Returns: 4

  39. 100000

    Returns: 3

  40. 1000

    Returns: 3

  41. 148437

    Returns: 5

  42. 300000

    Returns: 3

  43. 295240

    Returns: 1

  44. 299998

    Returns: 3

  45. 299997

    Returns: 4

  46. 299999

    Returns: 4

  47. 75

    Returns: 3

  48. 83

    Returns: 5


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: