diff options
| -rw-r--r-- | challenge-139/dave-jacoby/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-139/dave-jacoby/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-139/dave-jacoby/perl/ch-2.pl | 53 |
3 files changed, 86 insertions, 0 deletions
diff --git a/challenge-139/dave-jacoby/blog.txt b/challenge-139/dave-jacoby/blog.txt new file mode 100644 index 0000000000..d150a3b492 --- /dev/null +++ b/challenge-139/dave-jacoby/blog.txt @@ -0,0 +1 @@ +https://jacoby.github.io/2021/11/15/its-the-mullet-of-algorithms-the-weekly-challenge-139.html
\ No newline at end of file diff --git a/challenge-139/dave-jacoby/perl/ch-1.pl b/challenge-139/dave-jacoby/perl/ch-1.pl new file mode 100644 index 0000000000..78e5e644ad --- /dev/null +++ b/challenge-139/dave-jacoby/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say state postderef signatures }; +no warnings qw{ experimental }; + +# JortSort - https://jort.technology/ - https://github.com/jennschiffer/jortsort + +my @examples; +push @examples, [ 1 .. 5 ]; +push @examples, [ 1, 3, 2, 4, 5 ]; +push @examples, [ 1 .. 20 ]; +push @examples, [ sort { rand 1 <=> rand 1 } 1 .. 20 ]; + +for my $input (@examples) { + my $o = jortsort( $input->@* ); + my $i = join ',',$input->@*; + say <<"END"; + Input: \@n = ($i) + Output: $o +END +} + +# basically? It's sorted already, or go back and try again. +sub jortsort (@array ) { + my @copy = sort { $a <=> $b } @array; + for my $i ( 0 .. -1 + scalar @array ) { + return 0 if $copy[$i] ne $array[$i]; + } + return 1; +} diff --git a/challenge-139/dave-jacoby/perl/ch-2.pl b/challenge-139/dave-jacoby/perl/ch-2.pl new file mode 100644 index 0000000000..45bf5d7ae4 --- /dev/null +++ b/challenge-139/dave-jacoby/perl/ch-2.pl @@ -0,0 +1,53 @@ +#!/usr/bin/env perl + +use strict; +use warnings; +use feature qw{ say postderef signatures state }; +no warnings qw{ experimental }; + +# https://en.wikipedia.org/wiki/Full_reptend_prime + +# The first five long primes are: 7, 17, 19, 23, 29 + +use Math::BigFloat; +Math::BigFloat->accuracy(200); +my @long_primes; +for my $i ( 1 .. 50 ) { + next unless is_long_prime($i); + push @long_primes, $i; +} +say 'The first five Long Primes are: ', join ', ', @long_primes; + +sub is_long_prime ($n ) { + Math::BigFloat->accuracy($n*3); + return 0 unless is_prime($n); + my $bign = Math::BigFloat->new($n); + my $big1 = Math::BigFloat->new(1); + my $big = $big1->bdiv($bign); + $big =~ s/0+$//mix; + my $alt = $big; + $alt =~ s/^0\.//; + + my $l = $n - 1; + for my $i ( 1 .. $l ) { + my ( $f1, $f2 ) = $alt =~ m{(\d{$i})}g; + return 0 if !defined $f2; + return 0 if $f1 == $f2 && $i < $l; + return 0 if $f1 != $f2 && $i == $l; + } + + return 1; +} + +sub is_prime ( $n ) { + my @factors = factor($n); + return scalar @factors == 1 ? 1 : 0; +} + +sub factor ( $n ) { + my @factors; + for my $i ( 1 .. $n - 1 ) { + push @factors, $i if $n % $i == 0; + } + return @factors; +} |
