From 9b94fe8766e532231729219e74e7a05be81ce8a5 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 20 Nov 2021 13:15:26 +0100 Subject: Challenge 139 Task 1 2 LK Perl --- challenge-139/lubos-kolouch/perl/ch-1.pl | 22 +++++++++++++ challenge-139/lubos-kolouch/perl/ch-2.pl | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 challenge-139/lubos-kolouch/perl/ch-1.pl create mode 100644 challenge-139/lubos-kolouch/perl/ch-2.pl diff --git a/challenge-139/lubos-kolouch/perl/ch-1.pl b/challenge-139/lubos-kolouch/perl/ch-1.pl new file mode 100644 index 0000000000..f155ef7bda --- /dev/null +++ b/challenge-139/lubos-kolouch/perl/ch-1.pl @@ -0,0 +1,22 @@ +use strict; +use warnings; +use Data::Dumper; + +sub is_jort_sorted { + my $what = shift; + + my @sorted_arr = sort @$what; + + my $str1 = join ' ', @$what; + my $str2 = join ' ', @sorted_arr; + + return $str1 eq $str2 ? 1 : 0; + +} + +use Test::More; + +is( is_jort_sorted( [ 1, 2, 3, 4, 5 ] ), 1 ); +is( is_jort_sorted( [ 1, 3, 2, 4, 5 ] ), 0 ); + +done_testing; diff --git a/challenge-139/lubos-kolouch/perl/ch-2.pl b/challenge-139/lubos-kolouch/perl/ch-2.pl new file mode 100644 index 0000000000..9e7f17caa9 --- /dev/null +++ b/challenge-139/lubos-kolouch/perl/ch-2.pl @@ -0,0 +1,54 @@ +use strict; +use warnings; +use bignum ( p => -100 ); +use Math::Prime::Util qw/next_prime/; + +sub get_repeating_pattern { + my $what = shift; + + my $big_reverse = 1 / $what; + my $repeating = $1 if ($big_reverse) =~ /(.+?)\1/msx; + + return length($repeating); +} + +sub is_long_prime { + my $what = shift; + + my $repeats = get_repeating_pattern($what); + + return 1 if ( $repeats > 1 ) and ( $repeats == $what - 1 ); + + return 0; +} + +sub generate_long_primes { + + my $primes_count = 0; + my $at_prime = 0; + + my @result; + + while ( $primes_count < 5 ) { + $at_prime = next_prime($at_prime); + + if ( is_long_prime($at_prime) ) { + $primes_count++; + push @result, $at_prime; + } + } + + return \@result; +} + +use Test::More; + +is( get_repeating_pattern(7), 6 ); +is( get_repeating_pattern(17), 16 ); + +is( is_long_prime(7), 1 ); +is( is_long_prime(17), 1 ); +is( is_long_prime(2), 0 ); +is_deeply( generate_long_primes(), [ 7, 17, 19, 23, 29 ] ); + +done_testing; -- cgit