Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Write any natural number as sum of three triangular numbers

by BrowserUk (Patriarch)
on Sep 25, 2011 at 13:49 UTC ( [id://927748]=note: print w/replies, xml ) Need Help??


in reply to Write any natural number as sum of three triangular numbers

#! 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://927748]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-03-19 07:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found