Pollock's conjecture

Time limit1sMemory limit128 MB

Summary
For each integer below 10^6, find the fewest tetrahedral numbers that sum to it, and the fewest odd tetrahedral numbers that do.
Level

Medium7 of 10

Topics
Dynamic programming, Number theory, Math, Brute force
Solved
No attempts yet

Problem

The nn-th triangular number is the sum of the first nn positive integers. The nn-th tetrahedral number is the sum of the first nn triangular numbers, and it equals n(n+1)(n+2)6\frac{n(n+1)(n+2)}{6}. For example, the 5th tetrahedral number is 1+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+3+4+5)=5⋅6⋅76=351+(1+2)+(1+2+3)+(1+2+3+4)+(1+2+3+4+5)=\frac{5\cdot 6\cdot 7}{6}=35.

  • First 5 triangular numbers: 1,3,6,10,151, 3, 6, 10, 15
  • First 5 tetrahedral numbers: 1,4,10,20,351, 4, 10, 20, 35

In 1850, Sir Frederick Pollock, 1st Baronet — not a professional mathematician, but a British lawyer and politician — conjectured that every positive integer can be represented as the sum of at most five tetrahedral numbers. A tetrahedral number may appear in the sum more than once, and each occurrence is counted separately. The conjecture has remained open for more than a century and a half.

Write a program that, for each given integer, computes the least number of tetrahedral numbers whose sum equals that integer. In addition, compute the least number of terms when only odd tetrahedral numbers may be used.

For example, 40 can be written as the sum of 2 tetrahedral numbers, 20+2020+20 (where 20=4⋅5⋅6620=\frac{4\cdot 5\cdot 6}{6}), even though 40 itself is not a tetrahedral number. Using only odd tetrahedral numbers, 40 needs at least 6 terms, 35+1+1+1+1+135+1+1+1+1+1. So for the input 40 the program reports 2 and 6.

Input

The input consists of several lines, each containing a single positive integer less than 10610^6. The end of the input is indicated by a line containing a single 00.

Output

For each input integer, print one line with two integers separated by a single space. The first integer is the least number of tetrahedral numbers whose sum is the given integer. The second integer is the least number of odd tetrahedral numbers whose sum is the given integer. No extra characters should appear in the output.

Examples5

  1. Example 1

    Input
    40
    14
    5
    165
    120
    103
    106
    139
    0
    
    Expected output
    2 6
    2 14
    2 5
    1 1
    1 18
    5 35
    4 4
    3 37
    
  2. Example 2

    Input
    1
    0
    
    Expected output
    1 1
    
  3. Example 3

    Input
    2
    0
    
    Expected output
    2 2
    
  4. Example 4

    Input
    4
    0
    
    Expected output
    1 4
    
  5. Example 5

    Input
    35
    0
    
    Expected output
    1 1