diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-02-27 19:09:09 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-27 19:09:09 +0000 |
| commit | 52de2919f528a7351ba0c5c0da926b46b3d36aea (patch) | |
| tree | bef68b8892550d490154c8f86782083ea2bcd3d8 /challenge-153 | |
| parent | cc1550892f415de8424a0cc75344ddba9b09579a (diff) | |
| parent | ea7795614505d9e056e6b66a1e4de705228b9e9e (diff) | |
| download | perlweeklychallenge-club-52de2919f528a7351ba0c5c0da926b46b3d36aea.tar.gz perlweeklychallenge-club-52de2919f528a7351ba0c5c0da926b46b3d36aea.tar.bz2 perlweeklychallenge-club-52de2919f528a7351ba0c5c0da926b46b3d36aea.zip | |
Merge pull request #5712 from ccntrq/challenge-153
Challenge 153
Diffstat (limited to 'challenge-153')
| -rw-r--r-- | challenge-153/alexander-pankoff/blog1.txt | 1 | ||||
| -rw-r--r-- | challenge-153/alexander-pankoff/blog2.txt | 1 | ||||
| -rw-r--r-- | challenge-153/alexander-pankoff/haskell/ch-1.hs | 10 | ||||
| -rw-r--r-- | challenge-153/alexander-pankoff/haskell/ch-2.hs | 9 | ||||
| -rwxr-xr-x | challenge-153/alexander-pankoff/perl/ch-1.pl | 25 | ||||
| -rwxr-xr-x | challenge-153/alexander-pankoff/perl/ch-2.pl | 35 | ||||
| -rw-r--r-- | challenge-153/alexander-pankoff/raku/ch-1.raku | 9 | ||||
| -rw-r--r-- | challenge-153/alexander-pankoff/raku/ch-2.raku | 6 |
8 files changed, 96 insertions, 0 deletions
diff --git a/challenge-153/alexander-pankoff/blog1.txt b/challenge-153/alexander-pankoff/blog1.txt new file mode 100644 index 0000000000..66c2861a97 --- /dev/null +++ b/challenge-153/alexander-pankoff/blog1.txt @@ -0,0 +1 @@ +https://pankoff.net/pages/perl-weekly-challenge/challenge-153-task-1.html
\ No newline at end of file diff --git a/challenge-153/alexander-pankoff/blog2.txt b/challenge-153/alexander-pankoff/blog2.txt new file mode 100644 index 0000000000..c39f293e5b --- /dev/null +++ b/challenge-153/alexander-pankoff/blog2.txt @@ -0,0 +1 @@ +https://pankoff.net/pages/perl-weekly-challenge/challenge-153-task-2.html
\ No newline at end of file diff --git a/challenge-153/alexander-pankoff/haskell/ch-1.hs b/challenge-153/alexander-pankoff/haskell/ch-1.hs new file mode 100644 index 0000000000..293df8e922 --- /dev/null +++ b/challenge-153/alexander-pankoff/haskell/ch-1.hs @@ -0,0 +1,10 @@ +import Data.Function ( (&) ) + +main :: IO () +main = drop 1 leftFactorials & take 10 & print + +leftFactorials :: [Integer] +leftFactorials = 0 : zipWith (+) leftFactorials facs + +facs :: [Integer] +facs = 1 : zipWith (*) facs [1 ..]
\ No newline at end of file diff --git a/challenge-153/alexander-pankoff/haskell/ch-2.hs b/challenge-153/alexander-pankoff/haskell/ch-2.hs new file mode 100644 index 0000000000..dcad1370c6 --- /dev/null +++ b/challenge-153/alexander-pankoff/haskell/ch-2.hs @@ -0,0 +1,9 @@ +main :: IO () +main = print $ filter isFactorion [1 .. 100000] + +isFactorion :: Integer -> Bool +isFactorion 1 = True +isFactorion 2 = True +isFactorion 145 = True +isFactorion 40585 = True +isFactorion _ = False
\ No newline at end of file diff --git a/challenge-153/alexander-pankoff/perl/ch-1.pl b/challenge-153/alexander-pankoff/perl/ch-1.pl new file mode 100755 index 0000000000..5e8de04906 --- /dev/null +++ b/challenge-153/alexander-pankoff/perl/ch-1.pl @@ -0,0 +1,25 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw'say state signatures'; +no warnings qw'experimental::signatures'; + +use List::Util qw(sum0 product); + +run() unless caller(); + +sub run() { + say join( ', ', left_factorials( 1, 10 ) ); +} + +sub left_factorials ( $from, $to ) { + return map { left_factorial($_) } $from .. $to; +} + +sub left_factorial($n) { + return sum0( map { fac($_) } 0 .. ( $n - 1 ) ); +} + +sub fac($n) { + product( 1 .. $n ); +} diff --git a/challenge-153/alexander-pankoff/perl/ch-2.pl b/challenge-153/alexander-pankoff/perl/ch-2.pl new file mode 100755 index 0000000000..58a9b38cf1 --- /dev/null +++ b/challenge-153/alexander-pankoff/perl/ch-2.pl @@ -0,0 +1,35 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use feature qw'say state signatures'; +no warnings qw'experimental::signatures'; + +use List::Util qw(first sum0 product); + +run() unless caller(); + +sub run() { + my $max = 100000; + say "Factorions <= $max:"; + for ( 1 .. $max ) { + my $fn = ( \&is_factorion, \&is_factorion_a014080 )[ int( rand(2) ) ]; + say $_ if $fn->($_); + } +} + +sub is_factorion($n) { + my @digits = split( m//, $n ); + my $sum_of_factorials_of_digits = sum0( map { fac($_) } @digits ); + + return $n == $sum_of_factorials_of_digits; +} + +sub fac($n) { + product( 1 .. $n ); +} + +sub is_factorion_a014080($n) { + ## complete list of all factorians - see https://oeis.org/A014080 + state @A014080 = ( 1, 2, 145, 40585 ); + first { $n == $_ } @A014080; +} diff --git a/challenge-153/alexander-pankoff/raku/ch-1.raku b/challenge-153/alexander-pankoff/raku/ch-1.raku new file mode 100644 index 0000000000..e3f6f62c5f --- /dev/null +++ b/challenge-153/alexander-pankoff/raku/ch-1.raku @@ -0,0 +1,9 @@ +# I don't know how to write raku/perl6. With help from this blog post by Andrew +# Shitov I could figure out, how to generate a lazy, inifinite sequence of all +# factorials and adapt to the left factorial problem +# https://andrewshitov.com/2021/01/31/computing-factorials-using-raku/ + +my @facs = 1, * * ++$ ... *; +my @left_factorials = 0, 1, * + @facs[ ++$ ] ... *; + +say @left_factorials[1..10];
\ No newline at end of file diff --git a/challenge-153/alexander-pankoff/raku/ch-2.raku b/challenge-153/alexander-pankoff/raku/ch-2.raku new file mode 100644 index 0000000000..2864664246 --- /dev/null +++ b/challenge-153/alexander-pankoff/raku/ch-2.raku @@ -0,0 +1,6 @@ +unit sub MAIN(Int $n); +my @A014080 = 1, 2, 145, 40585; + +# Seems we have to coerce the argument $n into a number first by multiplying by +# 1. Didn't expect that. +say $n*1 ∈ @A014080;
\ No newline at end of file |
