diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-06-30 22:44:10 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-06-30 22:44:10 +0100 |
| commit | f70fdfee84f602971a4c6c144635883960f2c54f (patch) | |
| tree | 4884421464be2b25e10dfac958d9fafc39e1b319 | |
| parent | 58f5e220ae958fe48453e45fedc079df6b466d54 (diff) | |
| parent | 7755a024f5e0ed7581cce5bdaf7d1156e83fe076 (diff) | |
| download | perlweeklychallenge-club-f70fdfee84f602971a4c6c144635883960f2c54f.tar.gz perlweeklychallenge-club-f70fdfee84f602971a4c6c144635883960f2c54f.tar.bz2 perlweeklychallenge-club-f70fdfee84f602971a4c6c144635883960f2c54f.zip | |
Merge remote-tracking branch 'upstream/master'
89 files changed, 4275 insertions, 2313 deletions
diff --git a/challenge-007/kjetillll/perl/ch-1.pl b/challenge-007/kjetillll/perl/ch-1.pl new file mode 100644 index 0000000000..54f005bbd3 --- /dev/null +++ b/challenge-007/kjetillll/perl/ch-1.pl @@ -0,0 +1,6 @@ +#!/usr/bin/env perl +#https://theweeklychallenge.org/blog/perl-weekly-challenge-007/ +use v5.10; +use List::Util 'sum'; +my $upto = shift // 50; +say for grep $_ % sum(split//) == 0, 1..$upto; diff --git a/challenge-171/0rir/raku/ch-1.raku b/challenge-171/0rir/raku/ch-1.raku new file mode 100644 index 0000000000..0a852dff03 --- /dev/null +++ b/challenge-171/0rir/raku/ch-1.raku @@ -0,0 +1,44 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # ∅ ≡ ∩ ≢ +use v6.d; + +=begin comment +Task 1: Abundant Number Submitted by: Mohammad S Anwar + +Generate the first 20 Abundant Odd Numbers. An abundant number is +defined as a number n for which the sum of proper divisors > n. +Odd means odd. + +945 is the first Abundant Odd Number because the proper divisor summed +are greater than 945: +1 + 3 + 5 + 7 + 9 + 15 + 21 + 27 + 35 + 45 + 63 + 105 + 135 + 189 + 315 = 975 +=end comment + +sub proper-divisors (Int $n where $n > 0 --> Array ) { + # after the Rosetta code + my @result = 1 if $n > 1; + ( 2 .. $n.sqrt.Int ).map: -> $d { + unless $n % $d { + @result.push: $d; + my $y = $n div $d; + @result.push: $y if $y != $d + } + } + @result; +} + +constant @odd-abundant = gather { + my Int $i = 943; + loop { + $i += 2; + next if $i.is-prime; + if $i < [+] proper-divisors( $i) { + take $i; + next; + } + } +} + +say @odd-abundant[ 0 .. 19].join( ', '); +exit; + diff --git a/challenge-171/0rir/raku/ch-2.raku b/challenge-171/0rir/raku/ch-2.raku new file mode 100644 index 0000000000..a36c479e6f --- /dev/null +++ b/challenge-171/0rir/raku/ch-2.raku @@ -0,0 +1,48 @@ +#!/usr/bin/env raku +# :vim ft=raku sw=4 expandtab # ∅ ≡ ∩ ≢ +use v6.d; +use Test; + +=begin comment +Task 2: First-class Function Submitted by: Mohammad S Anwar + +Create sub compose($f, $g) which takes in two parameters $f and $g +as subroutine refs and returns subroutine ref i.e. compose($f, $g)->($x) = $f->($g->($x)) +The argument functions both are variadic ( one to ∞ ). + +After 'h = compose(f, g)' then +'f(g($x,$y, ... )) == h($x,$y, ...)' is True for any $x, $y, ... +=end comment + +sub compose( &one, &two ) { + sub ( *@a where @a[0].defined --> Array ) { one( two( @a )); } +} + +sub twice ( *@a where @a[0].defined --> Array) { [ @a.map: * × 2 ]; } + +sub incre( *@a is copy where @a[0].defined -->Array ) { + @a[$_] = @a[$_] +1 for 0 .. @a.end ; + @a; +} + + +my @test = [ 2, ], [ 2, 10], [ 0, 0], [ -2, -10, 100 ], [ -1, 1, 5, 9, 11, 15, 19, 23, 29]; + +my &both = compose( &twice, &incre) ; + + +plan 6 + @test.elems; + +dies-ok { incre() }, 'incre requires arg dies'; +dies-ok { twice() }, 'twice requires arg dies'; +dies-ok { both() }, 'both requires arg dies'; +is incre( 2, 10), [3, 11], 'incre( 2, 10)'; +is twice( 2, 10), [4, 20], 'twice( 2, 10)'; + +is both(2, 10, 20, 30), twice(incre(2, 10, 20, 30 )), + "both( 2, 10, 20, 30) ==> " ~ both( 2, 10, 20, 30) ~ ' hand'; + +for @test -> @t { + is both(@t), twice(incre( @t )), "both( @t[]) ==> " ~ both( @t); +} +done-testing; diff --git a/challenge-171/2colours/raku/ch-1.raku b/challenge-171/2colours/raku/ch-1.raku new file mode 100755 index 0000000000..f2e0dcc88d --- /dev/null +++ b/challenge-171/2colours/raku/ch-1.raku @@ -0,0 +1,12 @@ +#!/usr/bin/env raku + +sub divisors($num) { + my @small-divisors = (1 .. sqrt $num).grep: $num %% *; + (|@small-divisors, |($num <<div<< @small-divisors )).Set +} + +1, 3 ... * andthen + .grep: { .&divisors.keys.sum > 2 * $_ } andthen + .head: 20 andthen + .join: ', ' andthen + .say; diff --git a/challenge-171/2colours/raku/ch-2.raku b/challenge-171/2colours/raku/ch-2.raku new file mode 100755 index 0000000000..e767caecd7 --- /dev/null +++ b/challenge-171/2colours/raku/ch-2.raku @@ -0,0 +1,7 @@ +#!/usr/bin/env raku + +sub compose(&f, &g) { + sub (|args) { f(g(|args)) } +} + +[* * 3, &infix:<+>].reduce(&compose)(3, 4).say;
\ No newline at end of file diff --git a/challenge-171/brtastic/c/.gitignore b/challenge-171/brtastic/c/.gitignore new file mode 100644 index 0000000000..01f22977d8 --- /dev/null +++ b/challenge-171/brtastic/c/.gitignore @@ -0,0 +1,2 @@ +ch-1 + diff --git a/challenge-171/brtastic/c/ch-1.c b/challenge-171/brtastic/c/ch-1.c new file mode 100644 index 0000000000..fe097f1da2 --- /dev/null +++ b/challenge-171/brtastic/c/ch-1.c @@ -0,0 +1,35 @@ +#include <stdio.h> + +int sum_divisors (int number) +{ + int sum = 0; + int i; + for (i = 1; i <= number / 2; ++i) { + if (number % i == 0) + sum += i; + } + + return sum; +} + +int get_next_abundant_odd (int current) +{ + for (;; ++current) { + if (current % 2 != 0 && sum_divisors(current) > current) + return current; + } +} + +int main() +{ + int next = 0; + int i; + + for (i = 0; i < 20; ++i) { + next = get_next_abundant_odd(next + 1); + printf("%d\n", next); + } + + return 0; +} + diff --git a/challenge-171/brtastic/c/makefile b/challenge-171/brtastic/c/makefile new file mode 100644 index 0000000000..35e243f1c4 --- /dev/null +++ b/challenge-171/brtastic/c/makefile @@ -0,0 +1,6 @@ +compile: + cc ch-1.c -o ch-1 + +clean: + rm ch-1 + diff --git a/challenge-171/brtastic/pascal/.gitignore b/challenge-171/brtastic/pascal/.gitignore new file mode 100644 index 0000000000..7c16595d23 --- /dev/null +++ b/challenge-171/brtastic/pascal/.gitignore @@ -0,0 +1,3 @@ +*.o +ch-1 + diff --git a/challenge-171/brtastic/pascal/ch-1.pas b/challenge-171/brtastic/pascal/ch-1.pas new file mode 100644 index 0000000000..be683b5fce --- /dev/null +++ b/challenge-171/brtastic/pascal/ch-1.pas @@ -0,0 +1,35 @@ +program AbundantOddNumbers; + +{$mode objfpc}{$H+}{$J-} + +function SumDivisors(const vNumber: Integer): Integer; +var + vDivisor: Integer; +begin + result := 0; + + for vDivisor := 1 to vNumber div 2 do begin + if vNumber mod vDivisor = 0 then + result += vDivisor; + end; +end; + +function GetNextAbundantOddNumber(const vCurrent: Integer): Integer; +begin + result := vCurrent; + while (result mod 2 = 0) or (SumDivisors(result) <= result) do + result += 1; +end; + +var + vNext: Integer; + vInd: Integer; +begin + vNext := 0; + for vInd := 1 to 20 do begin + vNext := GetNextAbundantOddNumber(vNext + 1); + writeln(vNext); + end; + +end. + diff --git a/challenge-171/brtastic/pascal/makefile b/challenge-171/brtastic/pascal/makefile new file mode 100644 index 0000000000..93eeeeaf00 --- /dev/null +++ b/challenge-171/brtastic/pascal/makefile @@ -0,0 +1,6 @@ +compile: + fpc ch-1.pas + +clean: + rm ch-1.o ch-1 + diff --git a/challenge-171/brtastic/perl/ch-1.pl b/challenge-171/brtastic/perl/ch-1.pl new file mode 100755 index 0000000000..d3ed38efd3 --- /dev/null +++ b/challenge-171/brtastic/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!/usr/bin/env perl + +use v5.36; +use List::Util qw(sum0); + +sub get_divisors ($number) +{ + return grep { $number % $_ == 0 } 1 .. $number / 2; +} + +sub get_next_abundant_odd ($current = 1) +{ + while ('number is not abundant odd') { + return $current + if $current % 2 != 0 + && sum0(get_divisors $current) > $current + ; + + ++$current; + } +} + +my $current = 0; +for (1 .. 20) { + $current = get_next_abundant_odd($current + 1); + say $current; +} + diff --git a/challenge-171/brtastic/perl/ch-2.pl b/challenge-171/brtastic/perl/ch-2.pl new file mode 100755 index 0000000000..563cc7745b --- /dev/null +++ b/challenge-171/brtastic/perl/ch-2.pl @@ -0,0 +1,16 @@ +#!/usr/bin/env perl + +use v5.36; +use List::Util qw(shuffle); + +sub compose ($f1, $f2) +{ + return sub (@args) { + return $f1->($f2->(@args)); + }; +} + +my $sub_ref = compose(sub { local $, = ", "; say @_ }, \&shuffle); + +$sub_ref->(1 .. 20); + diff --git a/challenge-171/brtastic/raku/ch-1.raku b/challenge-171/brtastic/raku/ch-1.raku new file mode 100755 index 0000000000..13d26b7427 --- /dev/null +++ b/challenge-171/brtastic/raku/ch-1.raku @@ -0,0 +1,16 @@ +#!/usr/bin/env raku + +sub get-divisors (Int $for-number) returns Iterable +{ + return gather { + for 1 .. $for-number / 2 -> $current { + take $current if $for-number %% $current; + } + } +} + +sub MAIN() { + my $list = (1 .. *).hyper.grep({ !($_ %% 2) && get-divisors($_).sum > $_ }); + say $list[^20]; +} + diff --git a/challenge-171/dave-jacoby/perl/ch-1.pl b/challenge-171/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..45dd7bc0f3 --- /dev/null +++ b/challenge-171/dave-jacoby/perl/ch-1.pl |
