diff options
91 files changed, 5002 insertions, 2526 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/brxfork/perl/ch-1.sh b/challenge-171/brxfork/perl/ch-1.sh new file mode 100755 index 0000000000..8c1be89dcd --- /dev/null +++ b/challenge-171/brxfork/perl/ch-1.sh @@ -0,0 +1,2 @@ +#!/bin/sh +perl -E '$_="N"; $" = "+" ; while ($sol < 20) {$_.="NN";my @sum;/^(N+?)\1+$(?{push @sum,length$1})(?!)/;eval "@sum" > length() and ++$sol and say length()," < ", eval "@sum" , " = ", "@sum" }' diff --git a/challenge-171/dario-mazzeo/perl/ch-1.pl b/challenge-171/dario-mazzeo/perl/ch-1.pl new file mode 100755 index 0000000000..b3113fc87d --- /dev/null +++ b/challenge-171/dario-mazzeo/perl/ch-1.pl @@ -0,0 +1,23 @@ +# THE WEEKLY CHALLENGE - 171
+# Task 1: Abundant Number
+# Autore: Dario Mazzeo
+
+my $abu=1;
+my $num=0;
+do {
+ my $tot=0;
+ foreach my $div (1 .. 100){
+ if ($div<$abu && SeDivisore($abu,$div)==1){$tot+=$div;}
+ }
+ if ($tot>$abu){$num++; print "$abu\n";}
+ $abu++;
+} while ($num<20);
+exit;
+
+sub SeDivisore{
+my $a=$_[0];
+my $b=$_[1];
+
+if (($a % $b)==0){return 1;}
+else {return 0;}
+}
\ No newline at end of file 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) && (i % 2 != 0)) arr ~= i; + i++; + }while(arr.length != 20); + return arr; +} + +void main() +{ + int[] arr = abundant_odd_numbers(); + foreach(e; arr) + write(e, ' '); + writeln; +} diff --git a/challenge-171/deadmarshal/d/ch2.d b/challenge-171/deadmarshal/d/ch2.d new file mode 100644 index 0000000000..fb8b004af4 --- /dev/null +++ b/challenge-171/deadmarshal/d/ch2.d @@ -0,0 +1,10 @@ +import std.stdio:writeln; +import std.functional:compose; + +auto f = (int n) => n + 2; +auto g = (int n) => n * 2; + +void main() +{ + writeln(compose!(f, g)(5)); +} diff --git a/challenge-171/deadmarshal/haskell/ch-1.hs b/challenge-171/deadmarshal/haskell/ch-1.hs new file mode 100644 index 0000000000..9b5e59b5a7 --- /dev/null +++ b/challenge-171/deadmarshal/haskell/ch-1.hs @@ -0,0 +1,11 @@ +divisorsSum :: (Integral a) => a -> a +divisorsSum n = sum [x | x <- [1..(div n 2+1)], mod n x == 0] + +abundantOddNumbers :: (Integral a) => [ |
