aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-16 19:51:56 +0000
committerGitHub <noreply@github.com>2021-11-16 19:51:56 +0000
commit66d1e2943d3f15b0ad3430a60feb77871c60072d (patch)
treef87cf9528b96515f62f9b78068c84f584442babf
parent68dc7311718f5abfa5a0e5777ba74e51b500138f (diff)
parent2ea96380ba0513eb0722f4cceb944bb5a2db3caa (diff)
downloadperlweeklychallenge-club-66d1e2943d3f15b0ad3430a60feb77871c60072d.tar.gz
perlweeklychallenge-club-66d1e2943d3f15b0ad3430a60feb77871c60072d.tar.bz2
perlweeklychallenge-club-66d1e2943d3f15b0ad3430a60feb77871c60072d.zip
Merge pull request #5232 from choroba/ech139
Add solutions to 139: JortSort & Long Primes by E. Choroba
-rwxr-xr-xchallenge-139/e-choroba/perl/ch-1.pl29
-rwxr-xr-xchallenge-139/e-choroba/perl/ch-2.pl72
2 files changed, 101 insertions, 0 deletions
diff --git a/challenge-139/e-choroba/perl/ch-1.pl b/challenge-139/e-choroba/perl/ch-1.pl
new file mode 100755
index 0000000000..00117ffa7f
--- /dev/null
+++ b/challenge-139/e-choroba/perl/ch-1.pl
@@ -0,0 +1,29 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+
+sub jort_sort {
+ my @array = @_;
+
+ my @sorted = @array;
+ my $i = 0;
+ while ($i != $#sorted) {
+ if ($sorted[$i] <= $sorted[ $i + 1 ]) {
+ ++$i;
+ } else {
+ @sorted[$i + 1, $i] = @sorted[$i, $i + 1];
+ --$i unless 0 == $i;
+ }
+ }
+
+ for my $i (0 .. $#array) {
+ return 0 unless $array[$i] == $sorted[$i];
+ }
+ return 1
+}
+
+use Test2::V0;
+plan 2;
+
+is jort_sort(1, 2, 3, 4, 5), 1, 'Example 1';
+is jort_sort(1, 3, 2, 4, 5), 0, 'Example 2';
diff --git a/challenge-139/e-choroba/perl/ch-2.pl b/challenge-139/e-choroba/perl/ch-2.pl
new file mode 100755
index 0000000000..1282038b1b
--- /dev/null
+++ b/challenge-139/e-choroba/perl/ch-2.pl
@@ -0,0 +1,72 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+use feature qw{ say };
+
+sub prime_generator {
+ my @primes;
+ return sub {
+ if (! @primes) {
+ @primes = (2);
+ return 2
+ } elsif (1 == @primes) {
+ push @primes, 3;
+ return 3
+ }
+
+ my $candidate = $primes[-1];
+ CANDIDATE:
+ while (1) {
+ $candidate += 2;
+ for my $p (@primes) {
+ next CANDIDATE if 0 == $candidate % $p;
+ }
+ push @primes, $candidate;
+ return $candidate
+ }
+ }
+}
+
+sub is_long_prime {
+ my ($p) = @_;
+ my $inverted = '0.';
+ my $dividend = 1;
+ my $divisor = $p;
+ my %seen;
+ while ($dividend && $p + 1 >= length $inverted) {
+ return $p + 1 == length $inverted ? 1 : 0 if $seen{$dividend}++;
+
+ $dividend *= 10;
+ my $quotient = int($dividend / $divisor);
+ my $rest = $dividend % $divisor;
+ $inverted .= $quotient;
+ $dividend = $rest;
+ }
+ return 0
+}
+
+sub long_primes {
+ my ($n) = @_;
+ my @long_primes;
+ my $gen = prime_generator();
+ while ($n != @long_primes) {
+ my $p = $gen->();
+ push @long_primes, $p
+ if is_long_prime($p);
+ }
+ return @long_primes
+}
+
+say for long_primes(5);
+
+use Test2::V0;
+plan 1;
+
+my @LONG_PRIMES = (7, 17, 19, 23, 29, 47, 59, 61, 97, 109, 113, 131,
+ 149, 167, 179, 181, 193, 223, 229, 233, 257, 263,
+ 269, 313, 337, 367, 379, 383, 389, 419, 433, 461,
+ 487, 491, 499, 503, 509, 541, 571, 577, 593, 619,
+ 647, 659, 701, 709, 727, 743, 811, 821, 823, 857,
+ 863, 887, 937, 941, 953, 971, 977, 983);
+
+is [long_primes(60)], [@LONG_PRIMES], 'Wikipedia';