-
Notifications
You must be signed in to change notification settings - Fork 319
/
ch-1.pl
executable file
·44 lines (40 loc) · 953 Bytes
/
ch-1.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env perl
#===============================================================================
#
# FILE: 14701.pl
#
# USAGE: ./14701.pl
#
# DESCRIPTION: Output the first 20 left-truncatable prime numbers in
# base 10.
#
# REQUIREMENTS: Math::Prime::Util
# AUTHOR: Pete Houston (pete), cpan@openstrike.co.uk
# ORGANIZATION: Openstrike
# VERSION: 1.0
# CREATED: 10/01/22
#===============================================================================
use strict;
use warnings;
use Math::Prime::Util qw/next_prime/;
my $count = 20;
my $n = 1;
my @found;
my %primes;
while ($count > @found) {
$n = next_prime ($n);
$primes{$n} = 1;
push @found, $n if left_trunc ($n);
}
print "@found\n";
# This is a closure over %primes
sub left_trunc {
my $x = shift;
return if -1 < index $x, 0;
my $len = length ($x);
for (2 .. $len) {
$x = substr $x, 1;
return unless $primes{$x};
}
return 1;
}