diff options
Diffstat (limited to 'challenge-153')
93 files changed, 2466 insertions, 44 deletions
diff --git a/challenge-153/0rir/raku/ch-2.raku b/challenge-153/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..b6965b5958 --- /dev/null +++ b/challenge-153/0rir/raku/ch-2.raku @@ -0,0 +1,53 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab +use v6.d; +use MONKEY-SEE-NO-EVAL; + +constant \TEST=True; + +# Factorions + +$*ERR.print: "Warning: Postfix bang binds tighter than unary minus,\n" + ~ " Use a function for general use.\n"; + +my sub postfix:<!> ( Int $i --> Int ) { + state @f = 1,1,2,6; + while @f.end < $i { + @f.push: @f.end × (@f[@f.end -1] + @f[@f.end]) + } + @f[$i]; +} + +my $n = (1..100000).pick; + +factorion( $n); + +exit unless TEST; + +sub factorion ( Int $n, Bool :$QUIET --> Bool) { + my Str $explan = (($n.split( '', :skip-empty)).join: '! + ') ~ '!'; + my $output = EVAL( $explan); + my $ation = join ' + ', + ($explan.split( ' + ', :skip-empty)).map: { EVAL $_ }; + + unless $QUIET { + say "Input: \$n = $n\n" + ~ "Output: ", ($output==$n).Int(), "\n\n" + ~ " Since $explan => $ation = $output"; + } + $output == $n; +} + +use Test; + +my @good = 1, 2, 145, 40585; + +plan 50000; + +for @good -> $x { + is factorion($x), $x == @good.any, "is a factorion"; +} +for 0 .. 49999 { + next if $_ == @good.any; + is factorion($_, :QUIET ), False, 'is not a factorion'; +} diff --git a/challenge-153/adam-russell/blog.txt b/challenge-153/adam-russell/blog.txt new file mode 100644 index 0000000000..3e0711da3f --- /dev/null +++ b/challenge-153/adam-russell/blog.txt @@ -0,0 +1 @@ +http://www.rabbitfarm.com/cgi-bin/blosxom/perl/2022/02/27 diff --git a/challenge-153/adam-russell/perl/ch-1.pl b/challenge-153/adam-russell/perl/ch-1.pl new file mode 100644 index 0000000000..3104e9006b --- /dev/null +++ b/challenge-153/adam-russell/perl/ch-1.pl @@ -0,0 +1,24 @@ +use strict; +use warnings; +## +# Write a script to determine the first ten members of the Left Factorials sequence. +## +use POSIX; +use constant UPPER_BOUND => INT_MAX/1000; + +sub left_factorials_sieve{ + my($n) = @_; + my @sieve = (0 .. UPPER_BOUND); + my $x = 2; + { + my @sieve_indices = grep { $_ <= $x || $_ % $x == 0 } 0 .. @sieve - 1; + @sieve = map{ $sieve[$_] } @sieve_indices; + $x++; + redo if $x <= $n; + } + return @sieve[1 .. @sieve - 1]; +} + +MAIN:{ + print join(", ", left_factorials_sieve(10)) . "\n"; +}
\ No newline at end of file diff --git a/challenge-153/adam-russell/perl/ch-2.pl b/challenge-153/adam-russell/perl/ch-2.pl new file mode 100644 index 0000000000..7ab9492746 --- /dev/null +++ b/challenge-153/adam-russell/perl/ch-2.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +## +# Write a script to figure out if the given integer is a factorion. +## +use boolean; + +sub factorial{ + my($n) = @_; + return 1 if $n == 1; + $n * factorial($n - 1); +} + +sub is_factorion{ + my($n) = @_; + return boolean($n == unpack("%32I*", pack("I*", map {factorial($_)} split(//, $n)))); +} + +MAIN:{ + print is_factorion(145) . "\n"; + print is_factorion(123) . "\n"; +}
\ No newline at end of file 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 diff --git a/challenge-153/arne-sommer/blog.txt b/challenge-153/arne-sommer/blog.txt new file mode 100644 index 0000000000..141609a75d --- /dev/null +++ b/challenge-153/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/just-the-fact.html diff --git a/challenge-153/arne-sommer/raku/ch-1.raku b/challenge-153/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..d456dad9a9 --- /dev/null +++ b/challenge-153/arne-sommer/raku/ch-1.raku @@ -0,0 +1,7 @@ +#! /usr/bin/env raku + +unit sub MAIN (:c(:$count) = 10); + +my $lf := ( $ = 1, ( * + ([*] 1 .. ++$) ) ... Inf ); + +say $lf[^$count].join(", "); diff --git a/challenge-153/arne-sommer/raku/ch-2.raku b/challenge-153/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..61cc136615 --- /dev/null +++ b/challenge-153/arne-sommer/raku/ch-2.raku @@ -0,0 +1,5 @@ +#! /usr/bin/env raku + +unit sub MAIN (Int $n where $n > 0); + +say + ($n.comb.map({ [*] (1 .. $_) }).sum == $n); diff --git a/challenge-153/arne-sommer/raku/factorion-seq b/challenge-153/arne-sommer/raku/factorion-seq new file mode 100755 index 0000000000..21399295bc --- /dev/null +++ b/challenge-153/arne-sommer/raku/factorion-seq @@ -0,0 +1,7 @@ +#! /u |
