Statistics

Problem Statement for "HexagonalSums"

Problem Statement

Note to plugin users: this problem contains images which may not display properly with some plugins

Figurate numbers are numbers which form a scalable pattern of nested geometric figures. The hexagonal numbers (1,6,15,28,45,66,91,120,153...) form the following figures with points arranged in a plane as nested hexagons.

In the year 1830, the great mathematician, Legendre, proved that every integer larger than 1719 can be formed as the sum of exactly four hexagonal numbers (although many larger integers can also be formed as the sum of fewer than four hexagonal numbers). This was pretty much the last word on the subject until much later, in the year 1990, when this result was improved to three hexagonal numbers for all "sufficiently large" integers.

In this problem we ask the slightly different question: "what is the minimum number of hexagonal numbers that is required to form a particular sum?" Let's call this MLHS(n), for Minimum Length Hexagonal number Sum totaling to n. Here are the first few terms of MLHS(n):

n  MLHS(n)  a minimum length sum
1    1      1
2    2      1+1
3    3      1+1+1
4    4      1+1+1+1
5    5      1+1+1+1+1
6    1      6
7    2      1+6
8    3      1+1+6
9    4      1+1+1+6
10   5      1+1+1+1+6
11   6      1+1+1+1+1+6
12   2      6+6

For n greater than 1719, we know MLHS(n) <= 4 due to Legendre's result. But since our problem is different we can actually establish much tigher bounds. Six is the highest possible answer which only occurs for MLHS(11) and MLHS(26).
The largest n that has MLHS(n) = 6 is 26,
The largest n that has MLHS(n) = 5 is 130,
The largest n that has MLHS(n) = 4 is 146858.

Given a number, N, return MLHS(n), the minimum number of terms required to form a sum of N, when each term is a hexagonal number.

Definition

Class:
HexagonalSums
Method:
minLength
Parameters:
int
Returns:
int
Method signature:
int minLength(int N)
(be sure your method is public)

Constraints

  • N will be between 1 and 1000000 inclusive.

Examples

  1. 26

    Returns: 6

    1+1+6+6+6+6

  2. 130

    Returns: 5

    1+28+28+28+45

  3. 146858

    Returns: 4

    1+1+1326+145530

  4. 999999

    Returns: 3

    6+258840+741153

  5. 1000000

    Returns: 2

    285390+714610

  6. 145530

    Returns: 1

    145530 is the 269th hexagonal number

  7. 6

    Returns: 1

  8. 7

    Returns: 2

  9. 8

    Returns: 3

  10. 9

    Returns: 4

  11. 10

    Returns: 5

  12. 11

    Returns: 6

  13. 12

    Returns: 2

  14. 13

    Returns: 3

  15. 14

    Returns: 4

  16. 15

    Returns: 1

  17. 16

    Returns: 2

  18. 146859

    Returns: 2

  19. 20

    Returns: 5

  20. 25

    Returns: 5

  21. 38

    Returns: 5

  22. 39

    Returns: 5

  23. 54

    Returns: 5

  24. 65

    Returns: 5

  25. 70

    Returns: 5

  26. 114

    Returns: 5

  27. 1

    Returns: 1

  28. 999998

    Returns: 3

  29. 999997

    Returns: 3

  30. 999996

    Returns: 3

  31. 999995

    Returns: 3

  32. 123456

    Returns: 2

  33. 1326

    Returns: 1

  34. 2

    Returns: 2

  35. 3

    Returns: 3

  36. 4

    Returns: 4

  37. 5

    Returns: 5

  38. 666

    Returns: 2

  39. 19900

    Returns: 1

  40. 79800

    Returns: 1

  41. 719400

    Returns: 1

  42. 998992

    Returns: 2

  43. 1000000

    Returns: 2

  44. 45637

    Returns: 3

  45. 999998

    Returns: 3

  46. 26

    Returns: 6

  47. 999999

    Returns: 3

  48. 1111

    Returns: 3

  49. 190

    Returns: 1

  50. 18

    Returns: 3

  51. 997560

    Returns: 2

  52. 2

    Returns: 2

  53. 1

    Returns: 1


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: