My suggestion was to modify the recursive formula (function) to return not the sum value, but the paths themselves. Perhaps the more practical alternative is to construct an iterator. I've included both in the code below. Note that the @path have only column numbers. If you need Row-Column coordinates, zip/pairwise them with [0 .. $n] as appropriate.

#! /usr/bin/perl use Algorithm::Combinatorics q(:all); use Data::Dump; use strict; use warnings; push @ARGV, (6, 4); # default (n, k) # recursive formula # sub choose { my ($n, $k) = @_; !$n ? ([$k]) x !$k : map [@$_, $k], choose($n-1, $k), choose($n-1, + $k-1) } dd choose(@ARGV); # iterator version # sub pinball_iter { my ($n, $k) = @_; my $iter = combinations([1 .. $n], $k); sub { my @path = (0); $path[$_]++ for @{ $iter->next() // return }; $path[$_]+=$path[$_-1] for (1 .. $n); return \@path; } } my $it = pinball_iter(@ARGV); while (my $x = $it->()) { dd $x; }

The recursive formula is rather simple, and can be trivially adjusted for Row-Col pairs as well:

sub choose { my ($n, $k) = @_; !$n ? (["$n-$k"]) x !$k : map [@$_, "$n-$k"], choose($n-1, $k), choose($n-1, $k-1) }


In reply to Re: compute paths in Pascal's triangle (aka Tartaglia's one) by oiskuu
in thread compute paths in Pascal's triangle (aka Tartaglia's one) by Discipulus

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.