aboutsummaryrefslogtreecommitdiff
path: root/challenge-139
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-11-18 17:25:38 +0000
committerdrbaggy <js5@sanger.ac.uk>2021-11-18 17:25:38 +0000
commit913810214acef4eaeb99f1ccb16c4f03ebd1bd61 (patch)
tree8cde6838b2ac60e53f46c0c968313b5294cf5cd5 /challenge-139
parentb8efdcfb01a416ff48e80d4ef8c30e98039adde3 (diff)
downloadperlweeklychallenge-club-913810214acef4eaeb99f1ccb16c4f03ebd1bd61.tar.gz
perlweeklychallenge-club-913810214acef4eaeb99f1ccb16c4f03ebd1bd61.tar.bz2
perlweeklychallenge-club-913810214acef4eaeb99f1ccb16c4f03ebd1bd61.zip
added some notes
Diffstat (limited to 'challenge-139')
-rw-r--r--challenge-139/james-smith/perl/ch-2.pl38
1 files changed, 38 insertions, 0 deletions
diff --git a/challenge-139/james-smith/perl/ch-2.pl b/challenge-139/james-smith/perl/ch-2.pl
index c828aefea7..f431910856 100644
--- a/challenge-139/james-smith/perl/ch-2.pl
+++ b/challenge-139/james-smith/perl/ch-2.pl
@@ -6,14 +6,52 @@ use feature qw(say);
my( $N, @primes, @long_primes ) = ( $ARGV[0]||5 );
+##
+## We know 2 isn't a long prime, and also we are
+## only looking at odd numbers so we don't have
+## to worry about the special case of 2.
+##
+## Starting at the 3 we loop through the odd numbers
+## Looking to see if the number is composite {has
+## at least 1 prime factor lower than $p}.
+##
+## Skip to next number if it is (line 2)
+##
+## If it has the long_prime property we add it
+## to the list of long primes (line 3)
+## We also add it to the primes list (line 4)
+##
+## We exit the loop when we have the appropriate
+## number of long primes
+##
+
O: for( my $p=3; @long_primes<$N; $p+=2 ) {
($p % $_) || (next O) for @primes;
push @long_primes, $p if $p - rec_len($p) == 1;
push @primes, $p;
}
+
+##
+## Output the long primes
+##
+
say $_ for @long_primes;
+##
+## We use long division here to compute the reciprocal
+## of $D, to ensure we get a repeating pattern - we
+## compute the 2$D + length $D digits as the repeating
+## pattern will comprise of a number of 0s followed by
+## the repeating pattern (which has to be at least
+## 2$D-2 long.
+##
+## This division is done in line 2.
+##
+## The 3rd line finds the shortest repeating sequence
+## tied to the end of this string, and returns its
+## length
+##
sub rec_len {
my( $D, $N, $s ) = ( shift, 1, '' );
$s.=int($N/$D),$N%=$D,$N.=0 for 0..2*$D+length $D;