diff options
| author | E. Choroba <choroba@matfyz.cz> | 2021-11-15 23:33:43 +0100 |
|---|---|---|
| committer | E. Choroba <choroba@matfyz.cz> | 2021-11-15 23:33:43 +0100 |
| commit | 2ea96380ba0513eb0722f4cceb944bb5a2db3caa (patch) | |
| tree | 47baa84ca676c1650a7aadbe6fd3d95300890bda | |
| parent | 539750d62489e789280ec4d15a528f353e0c3baf (diff) | |
| download | perlweeklychallenge-club-2ea96380ba0513eb0722f4cceb944bb5a2db3caa.tar.gz perlweeklychallenge-club-2ea96380ba0513eb0722f4cceb944bb5a2db3caa.tar.bz2 perlweeklychallenge-club-2ea96380ba0513eb0722f4cceb944bb5a2db3caa.zip | |
Add solutions to 139: JortSort & Long Primes by E. Choroba
| -rwxr-xr-x | challenge-139/e-choroba/perl/ch-1.pl | 29 | ||||
| -rwxr-xr-x | challenge-139/e-choroba/perl/ch-2.pl | 72 |
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'; |
