diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2021-11-20 12:17:57 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-11-20 12:17:57 +0000 |
| commit | d8d283bd25f3b828c0ab35ac86470072302ceae5 (patch) | |
| tree | 6cc7fe7e6ec9807c7cc6fa369ca15d6c7922e8e0 | |
| parent | 75d6509b47d230723f43e00a05c8b90dcbddc7b5 (diff) | |
| parent | 9b94fe8766e532231729219e74e7a05be81ce8a5 (diff) | |
| download | perlweeklychallenge-club-d8d283bd25f3b828c0ab35ac86470072302ceae5.tar.gz perlweeklychallenge-club-d8d283bd25f3b828c0ab35ac86470072302ceae5.tar.bz2 perlweeklychallenge-club-d8d283bd25f3b828c0ab35ac86470072302ceae5.zip | |
Merge pull request #5252 from LubosKolouch/master
Challenge 139 Task 1 2 LK Perl
| -rw-r--r-- | challenge-139/lubos-kolouch/perl/ch-1.pl | 22 | ||||
| -rw-r--r-- | challenge-139/lubos-kolouch/perl/ch-2.pl | 54 |
2 files changed, 76 insertions, 0 deletions
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; |
