Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2024-04-24 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found