diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-12-06 10:31:54 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-06 10:31:54 +0000 |
| commit | 332582897bbd4788b431c3aefbce712a245ac540 (patch) | |
| tree | cb56c40e8650eda8c18e0764657b1455f47a71a6 | |
| parent | 935e74da3f8e99a084beac875fdf2e3003b1c05f (diff) | |
| parent | 04224b0f0d81675b8659b0d0bdc924e87e09ce0d (diff) | |
| download | perlweeklychallenge-club-332582897bbd4788b431c3aefbce712a245ac540.tar.gz perlweeklychallenge-club-332582897bbd4788b431c3aefbce712a245ac540.tar.bz2 perlweeklychallenge-club-332582897bbd4788b431c3aefbce712a245ac540.zip | |
Merge pull request #2917 from arnesom/branch-for-challenge-089
Arne Sommer 089
| -rw-r--r-- | challenge-089/arne-sommer/blog.txt | 1 | ||||
| -rwxr-xr-x | challenge-089/arne-sommer/perl/ch-1.pl | 54 | ||||
| -rwxr-xr-x | challenge-089/arne-sommer/perl/ch-2.pl | 37 | ||||
| -rwxr-xr-x | challenge-089/arne-sommer/perl/gcd-sum-perl | 54 | ||||
| -rwxr-xr-x | challenge-089/arne-sommer/perl/gcd-sum-perl-errorfix | 63 | ||||
| -rwxr-xr-x | challenge-089/arne-sommer/perl/magical-matrix-perl | 37 | ||||
| -rwxr-xr-x | challenge-089/arne-sommer/raku/ch-1.raku | 32 | ||||
| -rwxr-xr-x | challenge-089/arne-sommer/raku/ch-2.raku | 25 | ||||
| -rwxr-xr-x | challenge-089/arne-sommer/raku/gcd-sum | 32 | ||||
| -rwxr-xr-x | challenge-089/arne-sommer/raku/magical-matrix | 25 |
10 files changed, 360 insertions, 0 deletions
diff --git a/challenge-089/arne-sommer/blog.txt b/challenge-089/arne-sommer/blog.txt new file mode 100644 index 0000000000..e1169933c5 --- /dev/null +++ b/challenge-089/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/magical-sum.html diff --git a/challenge-089/arne-sommer/perl/ch-1.pl b/challenge-089/arne-sommer/perl/ch-1.pl new file mode 100755 index 0000000000..f831374a8b --- /dev/null +++ b/challenge-089/arne-sommer/perl/ch-1.pl @@ -0,0 +1,54 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; +use feature 'signatures'; +no warnings qw(experimental::signatures); + +use Algorithm::Combinatorics 'combinations'; +use List::MoreUtils 'duplicates'; +use Getopt::Long; + +my $verbose = 0; +GetOptions("verbose" => \$verbose); + +my $N = $ARGV[0] // die "Please specify an integer > 0"; + +die "Please specify an integer > 0" unless $N =~ /^[1-9]\d*$/; + +my @all = (1 .. $N); + +my $sum = 0; + +for my $pair (combinations(\@all, 2)) +{ + $sum += gcd(@$pair); +} + +say $sum; + +sub gcd ($a, $b) +{ + my @a = divisors($a); + my @b = divisors($b); + my @common = duplicates(@a, @b); + my $gcd = $common[$#common]; + + say ":: GCD($a, $b) -> $gcd" if $verbose; + + return $gcd; +} + +sub divisors ($number) +{ + my @divisors = (1); + + for my $candidate (2 .. $number/2) + { + push(@divisors, $candidate) if $number % $candidate == 0; + } + + push(@divisors, $number); + + return @divisors; +} diff --git a/challenge-089/arne-sommer/perl/ch-2.pl b/challenge-089/arne-sommer/perl/ch-2.pl new file mode 100755 index 0000000000..a1e6e58ae7 --- /dev/null +++ b/challenge-089/arne-sommer/perl/ch-2.pl @@ -0,0 +1,37 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; +use Getopt::Long; +use Algorithm::Combinatorics 'permutations'; + +my $verbose = 0; +my $all = 0; + +GetOptions("verbose" => \$verbose, + "all" => \$all); + +my @source = 1..9; + +for my $list (permutations(\@source)) +{ + my @candidate = @$list; + say ":: " . join(@candidate, ", ") if $verbose; + my ($a, $b, $c, $d, $e, $f, $g, $h, $i) = @candidate; + + next unless $a + $b + $c == 15; + next unless $d + $e + $f == 15; + next unless $g + $h + $i == 15; + next unless $a + $d + $g == 15; + next unless $b + $e + $h == 15; + next unless $c + $f + $i == 15; + next unless $a + $e + $i == 15; + next unless $c + $e + $g == 15; + + say "[ " . join(" ", @candidate[0..2]) . " ]"; + say "[ " . join(" ", @candidate[3..5]) . " ]"; + say "[ " . join(" ", @candidate[6..8]) . " ]"; + say "" if $all; + + last unless $all; +} diff --git a/challenge-089/arne-sommer/perl/gcd-sum-perl b/challenge-089/arne-sommer/perl/gcd-sum-perl new file mode 100755 index 0000000000..f831374a8b --- /dev/null +++ b/challenge-089/arne-sommer/perl/gcd-sum-perl @@ -0,0 +1,54 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; +use feature 'signatures'; +no warnings qw(experimental::signatures); + +use Algorithm::Combinatorics 'combinations'; +use List::MoreUtils 'duplicates'; +use Getopt::Long; + +my $verbose = 0; +GetOptions("verbose" => \$verbose); + +my $N = $ARGV[0] // die "Please specify an integer > 0"; + +die "Please specify an integer > 0" unless $N =~ /^[1-9]\d*$/; + +my @all = (1 .. $N); + +my $sum = 0; + +for my $pair (combinations(\@all, 2)) +{ + $sum += gcd(@$pair); +} + +say $sum; + +sub gcd ($a, $b) +{ + my @a = divisors($a); + my @b = divisors($b); + my @common = duplicates(@a, @b); + my $gcd = $common[$#common]; + + say ":: GCD($a, $b) -> $gcd" if $verbose; + + return $gcd; +} + +sub divisors ($number) +{ + my @divisors = (1); + + for my $candidate (2 .. $number/2) + { + push(@divisors, $candidate) if $number % $candidate == 0; + } + + push(@divisors, $number); + + return @divisors; +} diff --git a/challenge-089/arne-sommer/perl/gcd-sum-perl-errorfix b/challenge-089/arne-sommer/perl/gcd-sum-perl-errorfix new file mode 100755 index 0000000000..5319d133ba --- /dev/null +++ b/challenge-089/arne-sommer/perl/gcd-sum-perl-errorfix @@ -0,0 +1,63 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; +use feature 'signatures'; +no warnings qw(experimental::signatures); + +use Algorithm::Combinatorics 'combinations'; +# use List::MoreUtils 'duplicates'; +use Getopt::Long; + +my $verbose = 0; +GetOptions("verbose" => \$verbose); + +my $N = $ARGV[0] // die "Please specify an integer > 0"; + +die "Please specify an integer > 0" unless $N =~ /^[1-9]\d*$/; + +my @all = (1 .. $N); + +my $sum = 0; + +for my $pair (combinations(\@all, 2)) +{ + $sum += gcd(@$pair); +} + +say $sum; + +sub gcd ($a, $b) +{ + my @a = divisors($a); + my @b = divisors($b); + my @common = duplicates(@a, @b); + my $gcd = $common[$#common]; + + say ":: GCD($a, $b) -> $gcd" if $verbose; + + return $gcd; +} + +sub divisors ($number) +{ + my @divisors = (1); + + for my $candidate (2 .. $number/2) + { + push(@divisors, $candidate) if $number % $candidate == 0; + } + + push(@divisors, $number); + + return @divisors; +} + +sub duplicates (@) +{ + my %seen = (); + my $k; + my $seen_undef; + return grep { 1 < (defined $_ ? $seen{$k = $_} : $seen_undef) } + grep { defined $_ ? not $seen{$k = $_}++ : not $seen_undef++ } @_; +} diff --git a/challenge-089/arne-sommer/perl/magical-matrix-perl b/challenge-089/arne-sommer/perl/magical-matrix-perl new file mode 100755 index 0000000000..a1e6e58ae7 --- /dev/null +++ b/challenge-089/arne-sommer/perl/magical-matrix-perl @@ -0,0 +1,37 @@ +#! /usr/bin/env perl + +use strict; +use feature 'say'; +use Getopt::Long; +use Algorithm::Combinatorics 'permutations'; + +my $verbose = 0; +my $all = 0; + +GetOptions("verbose" => \$verbose, + "all" => \$all); + +my @source = 1..9; + +for my $list (permutations(\@source)) +{ + my @candidate = @$list; + say ":: " . join(@candidate, ", ") if $verbose; + my ($a, $b, $c, $d, $e, $f, $g, $h, $i) = @candidate; + + next unless $a + $b + $c == 15; + next unless $d + $e + $f == 15; + next unless $g + $h + $i == 15; + next unless $a + $d + $g == 15; + next unless $b + $e + $h == 15; + next unless $c + $f + $i == 15; + next unless $a + $e + $i == 15; + next unless $c + $e + $g == 15; + + say "[ " . join(" ", @candidate[0..2]) . " ]"; + say "[ " . join(" ", @candidate[3..5]) . " ]"; + say "[ " . join(" ", @candidate[6..8]) . " ]"; + say "" if $all; + + last unless $all; +} diff --git a/challenge-089/arne-sommer/raku/ch-1.raku b/challenge-089/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..457db81ee6 --- /dev/null +++ b/challenge-089/arne-sommer/raku/ch-1.raku @@ -0,0 +1,32 @@ +#! /usr/bin/env raku + +subset PositiveInt of Int where * > 0; + +unit sub MAIN (PositiveInt $N, :v(:$verbose)); + +say (1..$N).combinations(2).map( *.&gcd ).sum; + +sub gcd (@numbers) +{ + my %common = divisors(@numbers[0]) (&) divisors(@numbers[1]); + my @common = %common.keys.sort; + my $gcd = @common[* -1]; + + say ":: GCD(@numbers[0], @numbers[1]) -> $gcd" if $verbose; + + return $gcd; +} + +sub divisors ($number, :$not-self, :$not-one) +{ + my @divisors; + + for ($not-one ?? 2 !! 1) .. $number/2 -> $candidate + { + @divisors.push: $candidate if $number %% $candidate; + } + + @divisors.push: $number unless $not-self; + + return @divisors; +} diff --git a/challenge-089/arne-sommer/raku/ch-2.raku b/challenge-089/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..df60aba90f --- /dev/null +++ b/challenge-089/arne-sommer/raku/ch-2.raku @@ -0,0 +1,25 @@ +#! /usr/bin/env raku + +unit sub MAIN (:v(:$verbose), :a(:$all)); + +for (1..9).permutations -> @candidate +{ + say ":: @candidate[]" if $verbose; + my ($a, $b, $c, $d, $e, $f, $g, $h, $i) = @candidate; + + next unless $a + $b + $c == 15; + next unless $d + $e + $f == 15; + next unless $g + $h + $i == 15; + next unless $a + $d + $g == 15; + next unless $b + $e + $h == 15; + next unless $c + $f + $i == 15; + next unless $a + $e + $i == 15; + next unless $c + $e + $g == 15; + + say "[ { @candidate[0..2] } ]"; + say "[ { @candidate[3..5] } ]"; + say "[ { @candidate[6..8] } ]"; + say "" if $all; + + last unless $all; +}
\ No newline at end of file diff --git a/challenge-089/arne-sommer/raku/gcd-sum b/challenge-089/arne-sommer/raku/gcd-sum new file mode 100755 index 0000000000..457db81ee6 --- /dev/null +++ b/challenge-089/arne-sommer/raku/gcd-sum @@ -0,0 +1,32 @@ +#! /usr/bin/env raku + +subset PositiveInt of Int where * > 0; + +unit sub MAIN (PositiveInt $N, :v(:$verbose)); + +say (1..$N).combinations(2).map( *.&gcd ).sum; + +sub gcd (@numbers) +{ + my %common = divisors(@numbers[0]) (&) divisors(@numbers[1]); + my @common = %common.keys.sort; + my $gcd = @common[* -1]; + + say ":: GCD(@numbers[0], @numbers[1]) -> $gcd" if $verbose; + + return $gcd; +} + +sub divisors ($number, :$not-self, :$not-one) +{ + my @divisors; + + for ($not-one ?? 2 !! 1) .. $number/2 -> $candidate + { + @divisors.push: $candidate if $number %% $candidate; + } + + @divisors.push: $number unless $not-self; + + return @divisors; +} diff --git a/challenge-089/arne-sommer/raku/magical-matrix b/challenge-089/arne-sommer/raku/magical-matrix new file mode 100755 index 0000000000..df60aba90f --- /dev/null +++ b/challenge-089/arne-sommer/raku/magical-matrix @@ -0,0 +1,25 @@ +#! /usr/bin/env raku + +unit sub MAIN (:v(:$verbose), :a(:$all)); + +for (1..9).permutations -> @candidate +{ + say ":: @candidate[]" if $verbose; + my ($a, $b, $c, $d, $e, $f, $g, $h, $i) = @candidate; + + next unless $a + $b + $c == 15; + next unless $d + $e + $f == 15; + next unless $g + $h + $i == 15; + next unless $a + $d + $g == 15; + next unless $b + $e + $h == 15; + next unless $c + $f + $i == 15; + next unless $a + $e + $i == 15; + next unless $c + $e + $g == 15; + + say "[ { @candidate[0..2] } ]"; + say "[ { @candidate[3..5] } ]"; + say "[ { @candidate[6..8] } ]"; + say "" if $all; + + last unless $all; +}
\ No newline at end of file |
