diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-07-05 15:50:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-05 15:50:55 +0100 |
| commit | 731e9cae7e9405eb11d77d1e8b1d2e43fecbafb7 (patch) | |
| tree | 5e68bd7ebcf099f2c35527443dc285ffc86dd5ba /challenge-067 | |
| parent | f18956fe2d8d4b7dfc78a3bd5d3941fc8f863764 (diff) | |
| parent | 3f16527fb1c257355dc971ea8a1e26879aa73a86 (diff) | |
| download | perlweeklychallenge-club-731e9cae7e9405eb11d77d1e8b1d2e43fecbafb7.tar.gz perlweeklychallenge-club-731e9cae7e9405eb11d77d1e8b1d2e43fecbafb7.tar.bz2 perlweeklychallenge-club-731e9cae7e9405eb11d77d1e8b1d2e43fecbafb7.zip | |
Merge pull request #1904 from E7-87-83/master
Cheok Yin's submission for Task #1
Diffstat (limited to 'challenge-067')
| -rw-r--r-- | challenge-067/cheok-yin-fung/perl/ch-1.pl | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/challenge-067/cheok-yin-fung/perl/ch-1.pl b/challenge-067/cheok-yin-fung/perl/ch-1.pl new file mode 100644 index 0000000000..92dfa05e71 --- /dev/null +++ b/challenge-067/cheok-yin-fung/perl/ch-1.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl +use strict; +use Data::Dumper; + +# Usage: ch-1.pl [m] [n (also size of the combinations)] +# Main Idea: use the combinatorical identity C(n-1,r)+C(n-1,r-1)==C(n,r) + +my $m = 5; +my $n = 2; +if ($ARGV[0] and $ARGV[1]) { + $m = $ARGV[0]; + $n = $ARGV[1]; +} + +my @A =( [] , [[ [] ] ,[[1]]] , [ [ [] ], [ [1],[2] ], [ [1,2] ]] ); + +#my $x = Data::Dumper->new(\@A); # lines for testing +#print $x->Dump; +#print "\n"; + + +my $i = 3; +my $csize; #the size of the combination(s) + +while ($i<=$m) { + $A[$i][0] = []; + my @tempA1 = map {[$_]} (1..$i); + $A[$i][1] = \@tempA1; + $csize = 2; + while ($csize+1<=$i) { + my @temp0 = @{$A[$i-1][$csize]} ; + my @temp1 = map{ [ @{$_}, ] } @temp0; # C(n-1,r) + my @temp2 = @{$A[$i-1][$csize-1]}; # C(n-1,r-1) + push @temp1, map { [ @{$_} , $i] } @temp2; # addition + $A[$i][$csize] = \@temp1; + $csize++; + } + $A[$i][$i] =[[1..$i]]; + $i++; +} + +#$x = Data::Dumper->new(\@A); # lines for testing +#print $x->Dump; # lines for testing + +print "\$m = $m ; \$n = $n \n"; +print "\n"; + + +my @temp = @{ $A[$m][$n] }; +foreach my $arr (@temp) { + print "[", join ",", @{$arr}; print "]"; + print "\n"; +} + +print "\n"; |
