I looked at it from the "other side": get all the variations with repetition of the set of triangular numbers less than 80. Then compute their sums. Store these sums in a an array (yes it will overwrite previous results, but we only need one solution for each number) and print the results for the numbers upto 80.
use Modern::Perl; use Algorithm::Combinatorics qw /variations_with_repetition/; use List::Util qw /sum/; my @triangulars; push @triangulars, $_ * ( $_ + 1 ) / 2 for 0 .. 12; my $iterator = variations_with_repetition( \@triangulars, 3 ); my @results; $results[ sum @$_ ] = join ' + ', @$_ while $_ = $iterator->next; say "$_ = $results[$_]" for 0 .. 80;
Output:
0 = 0 + 0 + 0 1 = 1 + 0 + 0 2 = 1 + 1 + 0 3 = 3 + 0 + 0 4 = 3 + 1 + 0 5 = 3 + 1 + 1 6 = 6 + 0 + 0 7 = 6 + 1 + 0 8 = 6 + 1 + 1 9 = 6 + 3 + 0 10 = 10 + 0 + 0 11 = 10 + 1 + 0 12 = 10 + 1 + 1 13 = 10 + 3 + 0 14 = 10 + 3 + 1 15 = 15 + 0 + 0 16 = 15 + 1 + 0 17 = 15 + 1 + 1 18 = 15 + 3 + 0 19 = 15 + 3 + 1 20 = 10 + 10 + 0 21 = 21 + 0 + 0 22 = 21 + 1 + 0 23 = 21 + 1 + 1 24 = 21 + 3 + 0 25 = 21 + 3 + 1 26 = 15 + 10 + 1 27 = 21 + 6 + 0 28 = 28 + 0 + 0 29 = 28 + 1 + 0 30 = 28 + 1 + 1 31 = 28 + 3 + 0 32 = 28 + 3 + 1 33 = 21 + 6 + 6 34 = 28 + 6 + 0 35 = 28 + 6 + 1 36 = 36 + 0 + 0 37 = 36 + 1 + 0 38 = 36 + 1 + 1 39 = 36 + 3 + 0 40 = 36 + 3 + 1 41 = 28 + 10 + 3 42 = 36 + 6 + 0 43 = 36 + 6 + 1 44 = 28 + 15 + 1 45 = 45 + 0 + 0 46 = 45 + 1 + 0 47 = 45 + 1 + 1 48 = 45 + 3 + 0 49 = 45 + 3 + 1 50 = 28 + 21 + 1 51 = 45 + 6 + 0 52 = 45 + 6 + 1 53 = 28 + 15 + 10 54 = 45 + 6 + 3 55 = 55 + 0 + 0 56 = 55 + 1 + 0 57 = 55 + 1 + 1 58 = 55 + 3 + 0 59 = 55 + 3 + 1 60 = 45 + 15 + 0 61 = 55 + 6 + 0 62 = 55 + 6 + 1 63 = 45 + 15 + 3 64 = 55 + 6 + 3 65 = 55 + 10 + 0 66 = 66 + 0 + 0 67 = 66 + 1 + 0 68 = 66 + 1 + 1 69 = 66 + 3 + 0 70 = 66 + 3 + 1 71 = 55 + 15 + 1 72 = 66 + 6 + 0 73 = 66 + 6 + 1 74 = 45 + 28 + 1 75 = 66 + 6 + 3 76 = 66 + 10 + 0 77 = 66 + 10 + 1 78 = 78 + 0 + 0 79 = 78 + 1 + 0 80 = 78 + 1 + 1

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James


In reply to Re: Write any natural number as sum of three triangular numbers by CountZero
in thread Write any natural number as sum of three triangular numbers by ambrus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.