diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-29 20:12:55 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-29 20:12:55 +0100 |
| commit | 5e12a85b4bce52674823ea01ee24489211caccb3 (patch) | |
| tree | 3ba7ea82560bb66088a3980d0d7cec79dd969563 | |
| parent | 36d7d861775372e3c0a8ba9280b228f24e0d4ed1 (diff) | |
| parent | 937830a7990519a95e4f82842ebe225ab1e1aab3 (diff) | |
| download | perlweeklychallenge-club-5e12a85b4bce52674823ea01ee24489211caccb3.tar.gz perlweeklychallenge-club-5e12a85b4bce52674823ea01ee24489211caccb3.tar.bz2 perlweeklychallenge-club-5e12a85b4bce52674823ea01ee24489211caccb3.zip | |
Merge pull request #6365 from bbrtj/master
Challenge 171 solutions by Bartosz Jarzyna
| -rw-r--r-- | challenge-171/brtastic/c/.gitignore | 2 | ||||
| -rw-r--r-- | challenge-171/brtastic/c/ch-1.c | 35 | ||||
| -rw-r--r-- | challenge-171/brtastic/c/makefile | 6 | ||||
| -rw-r--r-- | challenge-171/brtastic/pascal/.gitignore | 3 | ||||
| -rw-r--r-- | challenge-171/brtastic/pascal/ch-1.pas | 35 | ||||
| -rw-r--r-- | challenge-171/brtastic/pascal/makefile | 6 | ||||
| -rwxr-xr-x | challenge-171/brtastic/perl/ch-1.pl | 28 | ||||
| -rwxr-xr-x | challenge-171/brtastic/perl/ch-2.pl | 16 | ||||
| -rwxr-xr-x | challenge-171/brtastic/raku/ch-1.raku | 16 |
9 files changed, 147 insertions, 0 deletions
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]; +} + |
