aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2022-06-04 00:23:43 +0100
committerGitHub <noreply@github.com>2022-06-04 00:23:43 +0100
commit7a6572ad3ea03fce0142e2229ffb00ec3f1b6dec (patch)
tree10e434eac8e7cbb9add27205d5d3cbd11136f96e
parent88c35c0a6961f834f04a467253599bc66eb46cfb (diff)
parentc4265d1953161d961309ec2b9aae01ca049b4f06 (diff)
downloadperlweeklychallenge-club-7a6572ad3ea03fce0142e2229ffb00ec3f1b6dec.tar.gz
perlweeklychallenge-club-7a6572ad3ea03fce0142e2229ffb00ec3f1b6dec.tar.bz2
perlweeklychallenge-club-7a6572ad3ea03fce0142e2229ffb00ec3f1b6dec.zip
Merge pull request #6195 from LubosKolouch/master
Challenge 167 Task 1 Perl
-rw-r--r--challenge-167/lubos-kolouch/perl/ch-1.pl52
1 files changed, 52 insertions, 0 deletions
diff --git a/challenge-167/lubos-kolouch/perl/ch-1.pl b/challenge-167/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..97c2199d8e
--- /dev/null
+++ b/challenge-167/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,52 @@
+use strict;
+use warnings;
+use Math::Prime::Util qw(is_prime next_prime);
+
+sub is_circular_prime {
+ my $prime = shift;
+
+ my $digits = length($prime);
+
+ for my $i ( 0 .. $digits - 1 ) {
+ return 0 unless is_prime($prime);
+ my $digit = substr( $prime, 0, 1 );
+ $prime = substr( $prime, 1, $digits - 1 ) . $digit;
+ }
+
+ return 1;
+}
+
+sub generate_primes {
+ my $limit = shift;
+
+ my @primes;
+ my $prime = next_prime(100);
+
+ while ( scalar @primes < $limit ) {
+ push @primes, $prime if is_circular_prime($prime);
+ $prime = next_prime($prime);
+ }
+
+ return \@primes;
+}
+
+use Test::More;
+
+is( is_circular_prime(2), 1, '2 is a circular prime' );
+is( is_circular_prime(3), 1, '3 is a circular prime' );
+is( is_circular_prime(11), 1, '11 is a circular prime' );
+is( is_circular_prime(13), 1, '13 is a circular prime' );
+is( is_circular_prime(57), 0, '57 not a circular prime' );
+is( is_circular_prime(197), 1, '197 is a circular prime' );
+
+# Note that the example given on the challenge is wrong according to
+# https://oeis.org/A068652
+
+is_deeply(
+ generate_primes(10),
+ [ 113, 131, 197, 199, 311, 337, 373, 719, 733, 919 ],
+
+ 'Generate primes'
+);
+
+done_testing();