diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-02-21 17:00:34 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-21 17:00:34 +0000 |
| commit | 8bd510cddc1d2e3597a935350795470af2b21326 (patch) | |
| tree | d21bd67980daabfb1dc59f95b4ee95099958a4eb | |
| parent | 8c9c80b7e468d2ddb56138bf5e2985876d99cd09 (diff) | |
| parent | 0a5175bd0c00c08d4d645e8d44f309e67fc0c965 (diff) | |
| download | perlweeklychallenge-club-8bd510cddc1d2e3597a935350795470af2b21326.tar.gz perlweeklychallenge-club-8bd510cddc1d2e3597a935350795470af2b21326.tar.bz2 perlweeklychallenge-club-8bd510cddc1d2e3597a935350795470af2b21326.zip | |
Merge pull request #5689 from pokgopun/c153
PWC153
| -rw-r--r-- | challenge-153/pokgopun/perl/ch-1.pl | 17 | ||||
| -rw-r--r-- | challenge-153/pokgopun/perl/ch-2.pl | 18 |
2 files changed, 35 insertions, 0 deletions
diff --git a/challenge-153/pokgopun/perl/ch-1.pl b/challenge-153/pokgopun/perl/ch-1.pl new file mode 100644 index 0000000000..03ae3e16b0 --- /dev/null +++ b/challenge-153/pokgopun/perl/ch-1.pl @@ -0,0 +1,17 @@ +use strict; +use warnings; + +my $res; +foreach my $n (1..10) { + ### Left factorial which is known by the other name as subfactorial but it does not produce output similar to the example + #$res .= sprintf "%s, ", eval(join(" + ", map{ &factorial($n) / &factorial($_) * (-1)**$_ } 0..$n )); + ### The below seems to produce output similar to the example + $res .= sprintf "%s, ", eval( join( " + ", map{&factorial($_)} 0..$n-1 ) ); +} +$res =~ s/\D+$//; +printf "%s\n", $res; +sub factorial { + my $n = shift; + return $n==0 ? 1 : eval(join(" * ", 1..$n)); +} + diff --git a/challenge-153/pokgopun/perl/ch-2.pl b/challenge-153/pokgopun/perl/ch-2.pl new file mode 100644 index 0000000000..a1e87a528b --- /dev/null +++ b/challenge-153/pokgopun/perl/ch-2.pl @@ -0,0 +1,18 @@ +use strict; +use warnings; + +my @n; +push @n, @ARGV ? @ARGV : (145,123); +foreach my $n (@n) { + my @digit = $n =~ /(\d)/g; + my $res0 = join(" + ", map{$_."!"} @digit); + my $res1 = join(" + ", map{&factorial($_)} @digit); + my $res = eval($res1) == $n ? 1 : 0; + + printf "Input: \$n = %s\nOutput: %s\n\n\tSince %s => %s %s %s\n\n", $n, $res, $res0, $res1, $res ? "=" : "<>", $n; +} +sub factorial { + my $n = shift; + return $n==0 ? 1 : eval(join(" * ", 1..$n)); +} + |
