aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-134/cheok-yin-fung/julia/ch-1.jl48
-rw-r--r--challenge-134/cheok-yin-fung/perl/ch-1.pl47
-rw-r--r--challenge-134/cheok-yin-fung/perl/ch-2.pl37
3 files changed, 132 insertions, 0 deletions
diff --git a/challenge-134/cheok-yin-fung/julia/ch-1.jl b/challenge-134/cheok-yin-fung/julia/ch-1.jl
new file mode 100644
index 0000000000..0b8bf5c3c9
--- /dev/null
+++ b/challenge-134/cheok-yin-fung/julia/ch-1.jl
@@ -0,0 +1,48 @@
+# The Weekly Challenge Week 134
+# Task 1 Pandigital Numbers
+# Usage: include("ch-1.jl")
+
+using Combinatorics
+
+tests = [1,5,21]
+
+
+
+function pandigital(a) # max: a <= 40320
+ if a > 40320
+ println("Sorry, to keep tiny, I only handle small cases.")
+ return
+ end
+ terms = []
+ for n = 1:a
+ push!(terms, join(nthperm([1,0,2,3,4,5,6,7,8,9], n), ""))
+ end
+ # sort!(terms)
+ for n = 1:a
+ println(terms[n])
+ end
+end
+
+
+
+for a in tests
+ pandigital(a)
+ println("")
+end
+
+
+# julia> include("ch-1.jl");
+#= output (separate by line instead of comma)
+
+ 1023456789
+
+ 1023456789, 1023456798, 1023456879,
+ 1023456897, 1023456978 # task requirement
+
+ 1023456789, 1023456798, 1023456879, 1023456897,
+ 1023456978, 1023456987, 1023457689, 1023457698,
+ 1023457869, 1023457896, 1023457968, 1023457986,
+ 1023458679, 1023458697, 1023458769, 1023458796,
+ 1023458967, 1023458976, 1023459678, 1023459687,
+ 1023459768 # OEIS showcase
+=#
diff --git a/challenge-134/cheok-yin-fung/perl/ch-1.pl b/challenge-134/cheok-yin-fung/perl/ch-1.pl
new file mode 100644
index 0000000000..064eb09917
--- /dev/null
+++ b/challenge-134/cheok-yin-fung/perl/ch-1.pl
@@ -0,0 +1,47 @@
+# The Weekly Challenge 134
+# Task 1 Pandigital Numbers
+# Usage: ch-1.pl [numbers of pandigital numbers]
+use v5.12;
+use warnings;
+use Test::More tests => 2;
+use Test::Deep;
+use Algorithm::Combinatorics qw(permutations);
+
+my $terms = $ARGV[0] || 5;
+
+my @arr = (1,0,2,3,4,5,6,7,8,9);
+
+my @factorial = (1, 1, 2, 6, 24, 120, 720, 5040, 40320);
+
+die "Sorry, I am lazy. Aren't you asking too much?\n"
+ if $terms > $factorial[8];
+
+say join "\n", run($terms)->@*;
+
+
+
+sub run {
+ my $t = $_[0];
+ my $ind = 0;
+ do $ind++ while $t > $factorial[$ind];
+
+ my @all_perm = permutations([ @arr[10-$ind..9] ]);
+ @all_perm = map { join "", @arr[0..9-$ind], @{$_}} @all_perm;
+ my @ans = @all_perm[0..$t-1];
+ # @ans = sort {$a<=>$b} @ans;
+ return [@ans];
+}
+
+
+
+cmp_deeply run(5),
+ [1023456789, 1023456798, 1023456879,
+ 1023456897, 1023456978], "task requirement";
+
+cmp_deeply run(21),
+ [1023456789, 1023456798, 1023456879, 1023456897,
+ 1023456978, 1023456987, 1023457689, 1023457698,
+ 1023457869, 1023457896, 1023457968, 1023457986,
+ 1023458679, 1023458697, 1023458769, 1023458796,
+ 1023458967, 1023458976, 1023459678, 1023459687,
+ 1023459768], "OEIS showcase";
diff --git a/challenge-134/cheok-yin-fung/perl/ch-2.pl b/challenge-134/cheok-yin-fung/perl/ch-2.pl
new file mode 100644
index 0000000000..13a5b71eec
--- /dev/null
+++ b/challenge-134/cheok-yin-fung/perl/ch-2.pl
@@ -0,0 +1,37 @@
+# The Weekly Challenge 134
+# Task 2 Distinct Terms Count
+# Usage: ch-2.pl $m $n (optional linewidth, default:256)
+use v5.12.0;
+use warnings;
+
+my $m = $ARGV[0] || 3;
+my $n = $ARGV[1] || 3;
+my $lw = $ARGV[2] || 256;
+
+my %hash;
+
+my $ref_prod = length $m*$n;
+my $ref_m = length $m;
+my $ref_long = $ref_m + 2 + $n * ( 1 + $ref_prod );
+die "The value of \$m*\$n is too large for linewidth $lw\n"
+ if $ref_long > $lw;
+
+print " " x ($ref_m - 1), "x", " |";
+printf "%*d", $ref_prod+1, $_ for (1..$n);
+print "\n";
+print "-" x ($ref_m+1), "+", "-" x ($ref_long-$ref_m-2);
+print "\n";
+for my $i (1..$m) {
+ printf "%*d |", $ref_m, $i;
+ for my $j (1..$n) {
+ printf "%*d", 1+$ref_prod, $i*$j;
+ $hash{$i*$j} = 1;
+ }
+ print "\n";
+}
+
+print "Distinct Terms:\n";
+print join ", ", sort {$a<=>$b} keys %hash;
+print "\n";
+print "Count: ", scalar keys %hash;
+print "\n";