diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-07 09:12:05 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-07 09:12:05 +0000 |
| commit | 9c64842638e7786a4c36df68f4418520cab033e0 (patch) | |
| tree | b45ad60f6dc2fef4f0f43503d9db0dda9602a3bc | |
| parent | 786f21aed588d59411c670d209123721bd492d57 (diff) | |
| parent | f51c76cb8ea94171452fbcc21e81c207b53581a2 (diff) | |
| download | perlweeklychallenge-club-9c64842638e7786a4c36df68f4418520cab033e0.tar.gz perlweeklychallenge-club-9c64842638e7786a4c36df68f4418520cab033e0.tar.bz2 perlweeklychallenge-club-9c64842638e7786a4c36df68f4418520cab033e0.zip | |
Merge pull request #5482 from arnesom/branch-for-challenge-146
Arne Sommer
| -rw-r--r-- | challenge-146/arne-sommer/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-146/arne-sommer/misc/cft.dot | 16 | ||||
| -rwxr-xr-x | challenge-146/arne-sommer/perl/10001st-perl | 22 | ||||
| -rwxr-xr-x | challenge-146/arne-sommer/perl/cft-perl | 28 | ||||
| -rwxr-xr-x | challenge-146/arne-sommer/perl/ch-1.pl | 22 | ||||
| -rwxr-xr-x | challenge-146/arne-sommer/perl/ch-2.pl | 28 | ||||
| -rwxr-xr-x | challenge-146/arne-sommer/raku/10001st | 7 | ||||
| -rwxr-xr-x | challenge-146/arne-sommer/raku/cft | 21 | ||||
| -rwxr-xr-x | challenge-146/arne-sommer/raku/cft-hash | 26 | ||||
| -rwxr-xr-x | challenge-146/arne-sommer/raku/ch-1.raku | 7 | ||||
| -rwxr-xr-x | challenge-146/arne-sommer/raku/ch-2.raku | 21 | ||||
| -rwxr-xr-x | challenge-146/arne-sommer/raku/mkcft | 40 |
12 files changed, 239 insertions, 0 deletions
diff --git a/challenge-146/arne-sommer/blog.txt b/challenge-146/arne-sommer/blog.txt new file mode 100644 index 0000000000..e830040152 --- /dev/null +++ b/challenge-146/arne-sommer/blog.txt @@ -0,0 +1 @@ +https://raku-musings.com/fractionally-prime.html diff --git a/challenge-146/arne-sommer/misc/cft.dot b/challenge-146/arne-sommer/misc/cft.dot new file mode 100644 index 0000000000..123624c159 --- /dev/null +++ b/challenge-146/arne-sommer/misc/cft.dot @@ -0,0 +1,16 @@ +digraph foogrph { + "1/3" -> "1/4"; + "1/3" -> "4/3"; + "3/2" -> "3/5"; + "3/2" -> "5/2"; + "1/2" -> "1/3"; + "1/2" -> "3/2"; + "2/3" -> "2/5"; + "2/3" -> "5/3"; + "3/1" -> "3/4"; + "3/1" -> "4/1"; + "2/1" -> "2/3"; + "2/1" -> "3/1"; + "1/1" -> "1/2"; + "1/1" -> "2/1"; +} diff --git a/challenge-146/arne-sommer/perl/10001st-perl b/challenge-146/arne-sommer/perl/10001st-perl new file mode 100755 index 0000000000..ec94aee83b --- /dev/null +++ b/challenge-146/arne-sommer/perl/10001st-perl @@ -0,0 +1,22 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +use Math::Prime::Util 'is_prime'; + +my $number = int($ARGV[0] || 10001); + +my $count = 0; +my $candidate = 0; + +while (++$candidate) +{ + next unless is_prime($candidate); + $count++; + + last if $count == $number; +} + +say $candidate; diff --git a/challenge-146/arne-sommer/perl/cft-perl b/challenge-146/arne-sommer/perl/cft-perl new file mode 100755 index 0000000000..0b016e495c --- /dev/null +++ b/challenge-146/arne-sommer/perl/cft-perl @@ -0,0 +1,28 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use feature 'signatures'; + +no warnings qw(experimental::signatures); + +my $fraction = $ARGV[0] // ""; + +die "Not a fraction: $fraction" unless $fraction =~ /^\d+\/\d+$/; + +my $parent = parent($fraction); +my $grandparent = parent($parent); + +say "parent = \'$parent\' and grandparent = \'$grandparent\'"; + +sub parent ($fraction) +{ + my ($numerator, $denominator) = split("/", $fraction); + + return "0/0" if $numerator == 1 && $denominator == 1; + + $numerator < $denominator + ? return $numerator . "/" . ( $denominator - $numerator ) + : return ($numerator - $denominator ) . "/" . $denominator; +} diff --git a/challenge-146/arne-sommer/perl/ch-1.pl b/challenge-146/arne-sommer/perl/ch-1.pl new file mode 100755 index 0000000000..ec94aee83b --- /dev/null +++ b/challenge-146/arne-sommer/perl/ch-1.pl @@ -0,0 +1,22 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; + +use Math::Prime::Util 'is_prime'; + +my $number = int($ARGV[0] || 10001); + +my $count = 0; +my $candidate = 0; + +while (++$candidate) +{ + next unless is_prime($candidate); + $count++; + + last if $count == $number; +} + +say $candidate; diff --git a/challenge-146/arne-sommer/perl/ch-2.pl b/challenge-146/arne-sommer/perl/ch-2.pl new file mode 100755 index 0000000000..0b016e495c --- /dev/null +++ b/challenge-146/arne-sommer/perl/ch-2.pl @@ -0,0 +1,28 @@ +#! /usr/bin/env perl + +use strict; +use warnings; +use feature 'say'; +use feature 'signatures'; + +no warnings qw(experimental::signatures); + +my $fraction = $ARGV[0] // ""; + +die "Not a fraction: $fraction" unless $fraction =~ /^\d+\/\d+$/; + +my $parent = parent($fraction); +my $grandparent = parent($parent); + +say "parent = \'$parent\' and grandparent = \'$grandparent\'"; + +sub parent ($fraction) +{ + my ($numerator, $denominator) = split("/", $fraction); + + return "0/0" if $numerator == 1 && $denominator == 1; + + $numerator < $denominator + ? return $numerator . "/" . ( $denominator - $numerator ) + : return ($numerator - $denominator ) . "/" . $denominator; +} diff --git a/challenge-146/arne-sommer/raku/10001st b/challenge-146/arne-sommer/raku/10001st new file mode 100755 index 0000000000..2bec5d731d --- /dev/null +++ b/challenge-146/arne-sommer/raku/10001st @@ -0,0 +1,7 @@ +#! /usr/bin/env raku + +unit sub MAIN (Int :$number where $number > 0 = 10001); + +my $primes := (1..Inf).grep( *.is-prime ); + +say $primes[$number -1];
\ No newline at end of file diff --git a/challenge-146/arne-sommer/raku/cft b/challenge-146/arne-sommer/raku/cft new file mode 100755 index 0000000000..bbfb954ea2 --- /dev/null +++ b/challenge-146/arne-sommer/raku/cft @@ -0,0 +1,21 @@ +#! /usr/bin/env raku + +subset NuDe of Str where * ~~ /^\d+\/\d+$/; + +unit sub MAIN (NuDe $fraction); + +my $parent = parent($fraction); +my $grandparent = parent($parent); + +say "parent = \'$parent\' and grandparent = \'$grandparent\'"; + +sub parent (NuDe $fraction) +{ + my (Int $numerator, Int $denominator) = $fraction.split("/")>>.Int; + + return "0/0" if $numerator == $denominator == 1; + + $numerator < $denominator + ?? return $numerator ~ "/" ~ $denominator - $numerator + !! return $numerator - $denominator ~ "/" ~ $denominator; +}
\ No newline at end of file diff --git a/challenge-146/arne-sommer/raku/cft-hash b/challenge-146/arne-sommer/raku/cft-hash new file mode 100755 index 0000000000..963ed8c806 --- /dev/null +++ b/challenge-146/arne-sommer/raku/cft-hash @@ -0,0 +1,26 @@ +#! /usr/bin/env raku + +subset NuDe of Str where * ~~ /^\d+\/\d+$/; + +unit sub MAIN (NuDe $fraction); + +my %parent = ( '1/4' => '1/3', + '4/3' => '1/3', + '3/5' => '3/2', + '5/2' => '3/2', + '1/3' => '1/2', + '3/2' => '1/2', + '2/5' => '2/3', + '5/3' => '2/3', + '3/4' => '3/1', + '4/1' => '3/1', + '2/3' => '2/1', + '3/1' => '2/1', + '1/2' => '1/1', + '2/1' => '1/1' + ); + +my $parent = %parent{$fraction} // die "No such member: $fraction"; +my $grandparent = %parent{$parent} // die "No such member: $parent"; + +say "parent = \'$parent\' and grandparent = \'$grandparent\'"; diff --git a/challenge-146/arne-sommer/raku/ch-1.raku b/challenge-146/arne-sommer/raku/ch-1.raku new file mode 100755 index 0000000000..2bec5d731d --- /dev/null +++ b/challenge-146/arne-sommer/raku/ch-1.raku @@ -0,0 +1,7 @@ +#! /usr/bin/env raku + +unit sub MAIN (Int :$number where $number > 0 = 10001); + +my $primes := (1..Inf).grep( *.is-prime ); + +say $primes[$number -1];
\ No newline at end of file diff --git a/challenge-146/arne-sommer/raku/ch-2.raku b/challenge-146/arne-sommer/raku/ch-2.raku new file mode 100755 index 0000000000..bbfb954ea2 --- /dev/null +++ b/challenge-146/arne-sommer/raku/ch-2.raku @@ -0,0 +1,21 @@ +#! /usr/bin/env raku + +subset NuDe of Str where * ~~ /^\d+\/\d+$/; + +unit sub MAIN (NuDe $fraction); + +my $parent = parent($fraction); +my $grandparent = parent($parent); + +say "parent = \'$parent\' and grandparent = \'$grandparent\'"; + +sub parent (NuDe $fraction) +{ + my (Int $numerator, Int $denominator) = $fraction.split("/")>>.Int; + + return "0/0" if $numerator == $denominator == 1; + + $numerator < $denominator + ?? return $numerator ~ "/" ~ $denominator - $numerator + !! return $numerator - $denominator ~ "/" ~ $denominator; +}
\ No newline at end of file diff --git a/challenge-146/arne-sommer/raku/mkcft b/challenge-146/arne-sommer/raku/mkcft new file mode 100755 index 0000000000..cdb7fb8d07 --- /dev/null +++ b/challenge-146/arne-sommer/raku/mkcft @@ -0,0 +1,40 @@ +#! /usr/bin/env raku + +subset NuDe of Str where * ~~ /^\d+\/\d+$/; + +unit sub MAIN (Int $levels where $levels > 0 = 3); + +say 'digraph foogrph {'; + +recurse('1/1'); + +say '}'; + +sub recurse (NuDe $current, $level = 1) +{ + my $left = left-child($current); + my $right = right-child($current); + + say " \"{ $current }\" -> \"{ left-child($current) }\""; + say " \"{ $current }\" -> \"{ right-child($current) }\""; + + return if $level == $levels; + + recurse($left, $level +1); + recurse($right, $level +1); +} + + +sub left-child (NuDe $fraction) +{ + my (Int $numerator, Int $denominator) = $fraction.split("/")>>.Int; + + return "$numerator/{ $numerator + $denominator }"; +} + +sub right-child (NuDe $fraction) +{ + my (Int $numerator, Int $denominator) = $fraction.split("/")>>.Int; + + return "{ $numerator + $denominator }/$denominator"; +} |
