aboutsummaryrefslogtreecommitdiff
path: root/challenge-067
diff options
context:
space:
mode:
authorFung Cheok Yin <61836418+E7-87-83@users.noreply.github.com>2020-07-05 22:37:14 +0800
committerGitHub <noreply@github.com>2020-07-05 22:37:14 +0800
commit3f16527fb1c257355dc971ea8a1e26879aa73a86 (patch)
tree8fc0fc70e7b1742e0ad751db9831506bd3ec4364 /challenge-067
parenta9d1f07a7e409306355cc46d8d27daaed0e81e53 (diff)
downloadperlweeklychallenge-club-3f16527fb1c257355dc971ea8a1e26879aa73a86.tar.gz
perlweeklychallenge-club-3f16527fb1c257355dc971ea8a1e26879aa73a86.tar.bz2
perlweeklychallenge-club-3f16527fb1c257355dc971ea8a1e26879aa73a86.zip
Create ch-1.pl
Diffstat (limited to 'challenge-067')
-rw-r--r--challenge-067/cheok-yin-fung/perl/ch-1.pl55
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";