Problem Statement
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
26
Returns: 6
1+1+6+6+6+6
130
Returns: 5
1+28+28+28+45
146858
Returns: 4
1+1+1326+145530
999999
Returns: 3
6+258840+741153
1000000
Returns: 2
285390+714610
145530
Returns: 1
145530 is the 269th hexagonal number
6
Returns: 1
7
Returns: 2
8
Returns: 3
9
Returns: 4
10
Returns: 5
11
Returns: 6
12
Returns: 2
13
Returns: 3
14
Returns: 4
15
Returns: 1
16
Returns: 2
146859
Returns: 2
20
Returns: 5
25
Returns: 5
38
Returns: 5
39
Returns: 5
54
Returns: 5
65
Returns: 5
70
Returns: 5
114
Returns: 5
1
Returns: 1
999998
Returns: 3
999997
Returns: 3
999996
Returns: 3
999995
Returns: 3
123456
Returns: 2
1326
Returns: 1
2
Returns: 2
3
Returns: 3
4
Returns: 4
5
Returns: 5
666
Returns: 2
19900
Returns: 1
79800
Returns: 1
719400
Returns: 1
998992
Returns: 2
1000000
Returns: 2
45637
Returns: 3
999998
Returns: 3
26
Returns: 6
999999
Returns: 3
1111
Returns: 3
190
Returns: 1
18
Returns: 3
997560
Returns: 2
2
Returns: 2
1
Returns: 1