Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

A non-recursive solution

by Thelonius (Priest)
on Aug 23, 2002 at 15:43 UTC ( [id://192363]=note: print w/replies, xml ) Need Help??


in reply to Combinatorics

#!perl -w use strict; # kenhirsch at myself.com 2002-08-23 my $r = shift or die "usage: combinations r a b c d e ...\n"; my @out = combinations($r, \@ARGV); for (@out) { print join(" ", @{$_}), "\n"; } # From Algorith L in Knuth Vol. 4 Sec 7.2.1.3 (not yet published) sub combinations { my ($t, $arrayref) = @_; my @c = 0 .. $t-1; my @range = reverse @c; my $j; my @result; $c[$t] = scalar(@{$arrayref}); $c[$t + 1] = 0; do { push @result, [@{$arrayref}[@c[@range]]]; for ($j=0; $c[$j] + 1 == $c[$j+1]; $j++) { $c[$j] = $j; } $c[$j]++; } while ($j < $t); return @result; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-04-18 06:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found