#! perl -slw use strict; my @trinums = map $_ * ($_-1) /2, 1 .. 15; my %trinums; undef @trinums{ @trinums }; sub triSum { my $n = shift; for my $f ( 0 .. $#trinums ) { my $first = $trinums[ $f ]; for my $s ( 0 .. $first - 1, $first + 1 .. $#trinums ) { my $second = $trinums[ $s ]; my $third = $n - ( $first + $second ); return $first, $second, $third if exists $trinums{ $third +}; } } } print "$_ : ", join ' + ', triSum( $_ ) for 1 .. 80; __END__ c:\test>trinums 1 : 0 + 1 + 0 2 : 0 + 1 + 1 3 : 0 + 3 + 0 4 : 0 + 1 + 3 5 : 1 + 3 + 1 6 : 0 + 3 + 3 7 : 0 + 1 + 6 8 : 1 + 6 + 1 9 : 0 + 3 + 6 10 : 0 + 10 + 0 11 : 0 + 1 + 10 12 : 0 + 6 + 6 13 : 0 + 3 + 10 14 : 1 + 3 + 10 15 : 0 + 15 + 0 16 : 0 + 1 + 15 17 : 1 + 6 + 10 18 : 0 + 3 + 15 19 : 1 + 3 + 15 20 : 0 + 10 + 10 21 : 0 + 6 + 15 22 : 0 + 1 + 21 23 : 1 + 21 + 1 24 : 0 + 3 + 21 25 : 0 + 10 + 15 26 : 1 + 10 + 15 27 : 0 + 6 + 21 28 : 0 + 28 + 0 29 : 0 + 1 + 28 30 : 0 + 15 + 15 31 : 0 + 3 + 28 32 : 1 + 3 + 28 33 : 3 + 15 + 15 34 : 0 + 6 + 28 35 : 1 + 6 + 28 36 : 0 + 15 + 21 37 : 0 + 1 + 36 38 : 0 + 10 + 28 39 : 0 + 3 + 36 40 : 1 + 3 + 36 41 : 3 + 10 + 28 42 : 0 + 6 + 36 43 : 0 + 15 + 28 44 : 1 + 15 + 28 45 : 0 + 45 + 0 46 : 0 + 1 + 45 47 : 1 + 10 + 36 48 : 0 + 3 + 45 49 : 0 + 21 + 28 50 : 1 + 21 + 28 51 : 0 + 6 + 45 52 : 1 + 6 + 45 53 : 10 + 15 + 28 54 : 3 + 15 + 36 55 : 0 + 10 + 45 56 : 0 + 1 + 55 57 : 0 + 21 + 36 58 : 0 + 3 + 55 59 : 1 + 3 + 55 60 : 0 + 15 + 45 61 : 0 + 6 + 55 62 : 1 + 6 + 55 63 : 3 + 15 + 45 64 : 0 + 28 + 36 65 : 0 + 10 + 55 66 : 0 + 21 + 45 67 : 0 + 1 + 66 68 : 1 + 66 + 1 69 : 0 + 3 + 66 70 : 0 + 15 + 55 71 : 1 + 15 + 55 72 : 0 + 6 + 66 73 : 0 + 28 + 45 74 : 1 + 28 + 45 75 : 3 + 36 + 36 76 : 0 + 10 + 66 77 : 1 + 10 + 66 78 : 0 + 78 + 0 79 : 0 + 1 + 78 80 : 1 + 78 + 1

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re: Write any natural number as sum of three triangular numbers by BrowserUk
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.