diff options
| -rw-r--r-- | challenge-139/paulo-custodio/perl/ch-1.pl | 32 | ||||
| -rw-r--r-- | challenge-139/paulo-custodio/perl/ch-2.pl | 58 | ||||
| -rw-r--r-- | challenge-139/paulo-custodio/t/test-1.yaml | 10 | ||||
| -rw-r--r-- | challenge-139/paulo-custodio/t/test-2.yaml | 10 |
4 files changed, 110 insertions, 0 deletions
diff --git a/challenge-139/paulo-custodio/perl/ch-1.pl b/challenge-139/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..b182d12cb4 --- /dev/null +++ b/challenge-139/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,32 @@ +#!/usr/bin/env perl + +# TASK #1 > JortSort +# Submitted by: Mohammad S Anwar +# You are given a list of numbers. +# +# Write a script to implement JortSort. It should return true/false depending +# if the given list of numbers are already sorted. +# +# Example 1: +# Input: @n = (1,2,3,4,5) +# Output: 1 +# +# Since the array is sorted, it prints 1. +# Example 2: +# Input: @n = (1,3,2,4,5) +# Output: 0 +# +# Since the array is NOT sorted, it prints 0. + +use Modern::Perl; + +sub jortsort { + my(@nums) = @_; + my @sorted = sort {$a<=>$b} @nums; + for (0..$#nums) { + return 0 if $nums[$_]!=$sorted[$_]; + } + return 1; +} + +say jortsort(@ARGV); diff --git a/challenge-139/paulo-custodio/perl/ch-2.pl b/challenge-139/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..4404e1f67d --- /dev/null +++ b/challenge-139/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,58 @@ +#!/usr/bin/env perl + +# TASK #2 > Long Primes +# Submitted by: Mohammad S Anwar +# Write a script to generate first 5 Long Primes. +# +# A prime number (p) is called Long Prime if (1/p) has an infinite decimal +# expansion repeating every (p-1) digits. +# +# Example +# 7 is a long prime since 1/7 = 0.142857142857... +# The repeating part (142857) size is 6 i.e. one less than the prime number 7. +# +# Also 17 is a long prime since 1/17 = 0.05882352941176470588235294117647... +# The repeating part (0588235294117647) size is 16 i.e. one less than the +# prime number 17. +# +# Another example, 2 is not a long prime as 1/2 = 0.5. +# There is no repeating part in this case. + +use Modern::Perl; +use Math::BigFloat; +use ntheory 'next_prime'; + +Math::BigFloat->accuracy(1000); # very long list of digits + +my $N = shift||5; +my $prime = 2; +for (1..$N) { + while (!is_long_prime($prime)) { + $prime = next_prime($prime); + } + say $prime; + $prime = next_prime($prime); +} + +sub is_long_prime { + my($p) = @_; + my $inv = Math::BigFloat->new(1) / Math::BigFloat->new($p); + if (rept_sequence($inv, $p-1)==$p-1) { + return 1; + } + else { + return 0; + } +} + +sub rept_sequence { + my($n, $max) = @_; + for my $rept (1..$max) { + my $rept1 = $rept-1; + my $code = "\$n =~ /\\.(\\d{$rept})\\1+\\d{0,$rept1}\$/;"; + if (eval $code) { + return $rept; + } + } + return -1; +} diff --git a/challenge-139/paulo-custodio/t/test-1.yaml b/challenge-139/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..ff33b5b648 --- /dev/null +++ b/challenge-139/paulo-custodio/t/test-1.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 1 2 3 4 5 + input: + output: 1 +- setup: + cleanup: + args: 1 3 2 4 5 + input: + output: 0 diff --git a/challenge-139/paulo-custodio/t/test-2.yaml b/challenge-139/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..cc94e5612a --- /dev/null +++ b/challenge-139/paulo-custodio/t/test-2.yaml @@ -0,0 +1,10 @@ +- setup: + cleanup: + args: 5 + input: + output: | + 7 + 17 + 19 + 23 + 29 |
