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 /challenge-171 | |
| 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'
Diffstat (limited to 'challenge-171')
73 files changed, 1699 insertions, 0 deletions
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 @@ -0,0 +1,44 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +use List::Util qw{ sum0 }; + +$| = 1; + +# abundant odd number +my @abundant; +my $iter = make_iterator(945); +while ( my $i = $iter->() ) { + next if $i % 2 == 0; + next unless is_abundant($ i ); + push @abundant, $i; + last if scalar @abundant >20; +} + +say join ' ', @abundant; + +sub is_abundant( $n ) { + my @factors = get_factors($n); + my $sum = sum0 @factors; + return $sum > $n ? 1 : 0; +} + +sub get_factors( $n ) { + my @factors; + for my $i ( 1 .. 1 + $n / 2 ) { + next unless $n % $i == 0; + push @factors, $i; + } + return @factors; +} + +sub make_iterator($n) { + return sub { + state $v = $n; + return $v++; + } +} + diff --git a/challenge-171/dave-jacoby/perl/ch-2.pl b/challenge-171/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..3c4cddf2c7 --- /dev/null +++ b/challenge-171/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,23 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use experimental qw{ say postderef signatures state }; + +my $f = sub ( $sub ) { + return sub ( $x ) { return uc $sub->($x) } +}; + +my $g = sub ( $val ) { + return join ' ', map { $val } 1 .. 3; +}; + +my $h = compose( $f, $g ); +say join " / ", $h->(qw{ sub refs are fun }); + +sub compose ( $f, $g ) { + my $sub = $f->($g); + return sub ( @x ) { + return map { $sub->($_) } @x; + } +} diff --git a/challenge-171/deadmarshal/cpp/ch-1.cpp b/challenge-171/deadmarshal/cpp/ch-1.cpp new file mode 100644 index 0000000000..388cb0b7a4 --- /dev/null +++ b/challenge-171/deadmarshal/cpp/ch-1.cpp @@ -0,0 +1,34 @@ +#include<iostream> +#include<vector> +#include<cmath> + +template<typename T> +T divisors_sum(T n) +{ + T sum{}; + for(T i = 1; i < n / 2+1; ++i) + if(n % i == 0) sum += i; + return sum; +} + +template<typename T> +std::vector<T> abundant_odd_numbers() +{ + std::vector<T> vec{}; + int i = 1; + do{ + if((divisors_sum(i) > i) && (i % 2 != 0)) vec.push_back(i); + i++; + }while(vec.size() != 20); + return vec; +} + +int main() +{ + std::vector<int> vec = abundant_odd_numbers<int>(); + for(const auto& e : vec) + std::cout << e << ' '; + std::cout << '\n'; + return 0; +} + diff --git a/challenge-171/deadmarshal/cpp/ch-2.cpp b/challenge-171/deadmarshal/cpp/ch-2.cpp new file mode 100644 index 0000000000..ff23f617ed --- /dev/null +++ b/challenge-171/deadmarshal/cpp/ch-2.cpp @@ -0,0 +1,17 @@ +#include<iostream> +#include<functional> + +std::function<int(int)> f = [](auto n){return n + 2;}; +std::function<int(int)> g = [](auto n){return n * 2;}; + +template<typename F, typename G> +auto compose(F f, G g) +{ + return [f,g](auto n){return f(g(n));}; +} + +int main() +{ + std::cout << compose(f, g)(5) << '\n'; + return 0; +} diff --git a/challenge-171/deadmarshal/d/ch1.d b/challenge-171/deadmarshal/d/ch1.d new file mode 100644 index 0000000000..670b38fc1e --- /dev/null +++ b/challenge-171/deadmarshal/d/ch1.d @@ -0,0 +1,28 @@ +import std.stdio:writeln,write; + +int divisors_sum(int n) +{ + int sum = 0; + for(int i = 1; i < n / 2+1; ++i) + if(n % i == 0) sum += i; + return sum; +} + +int[] abundant_odd_numbers() +{ + int[] arr; + int i = 1; + do{ + if((divisors_sum(i) > i) &am |
