From 60f80adda58560bfabecf16e825058b2ac227c42 Mon Sep 17 00:00:00 2001 From: James Smith Date: Wed, 17 Nov 2021 10:38:10 +0000 Subject: Update README.md --- challenge-139/james-smith/README.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/challenge-139/james-smith/README.md b/challenge-139/james-smith/README.md index b1e418d85e..12cbf5c99d 100644 --- a/challenge-139/james-smith/README.md +++ b/challenge-139/james-smith/README.md @@ -26,7 +26,7 @@ This challenge is relatively easy - to see if the list of numbers if monotonical * Otherwise we set previous number `$p` to the current number and continue * If we get to the end of the list then the list is sorted and we return `1`. -``` +```perl sub in_order { my $p = shift; ($p>$_) ? (return 0) : ($p=$_) for @_; @@ -38,7 +38,7 @@ sub in_order { * We can rewrite the `if( $x ) { y } else { z }` and `($x) ? (y) : (z)`. Why is this useful - well we can then use the brace less postfix `for` for the loop rather than having to use braces. This means the loop becomes 1 line, rather than the longer 7 line version using K&R braces. If you don't cuddle your braces it is even longer! -``` +```perl for (@_) { if( $p>$_ ) { return 0; @@ -50,7 +50,7 @@ sub in_order { Admittedly there is an intermediate version... That uses the exit early approach.. -``` +```perl for (@_) { return 0 if $p>$_; $p = $_; @@ -74,7 +74,7 @@ Now we don't require the actual part of the number repeats which makes the funct This gives us the function below to get the length of the recurring sequence. -``` +```perl sub rec_len { my( $D, $N, $s ) = ( shift, 1, '' ); ( $s, $N ) = ( $s.int($N/$D), ($N%$D).0 ) for 0 .. 2*$D; @@ -89,7 +89,7 @@ So now we have this function we can look at computing the long primes. We know t Therefore we loop through all the odd numbers checking to see if the number is a prime, if it is we then check for the property that the recurring sequence has `$p-1` digits. -``` +```perl my( $N, @primes, @long_primes ) = ( $ARGV[0]||5 ); O: for( my $p=3; @long_primes<$N; $p+=2 ) { -- cgit From f748a7d32405d399712853441aa1c9906732bd3a Mon Sep 17 00:00:00 2001 From: drbaggy Date: Thu, 18 Nov 2021 09:26:41 +0000 Subject: tidied up code --- challenge-139/james-smith/perl/ch-2.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/challenge-139/james-smith/perl/ch-2.pl b/challenge-139/james-smith/perl/ch-2.pl index 8311c8a438..cd0c2c56ca 100644 --- a/challenge-139/james-smith/perl/ch-2.pl +++ b/challenge-139/james-smith/perl/ch-2.pl @@ -16,6 +16,6 @@ say $_ for @long_primes; sub rec_len { my( $D, $N, $s ) = ( shift, 1, '' ); - ($s,$N) = ( $s.int($N/$D), ($N%$D).0 ) for 0 .. 2*$D; - $s =~ /(\d+?)\1+$/ ? length $1 : 0; + $s.=int($N/$D),$N%=$D,$N.=0 for 0..2*$D; + $s =~ /(.+?)\1+$/ ? length $1 : 0; } -- cgit From b8efdcfb01a416ff48e80d4ef8c30e98039adde3 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Thu, 18 Nov 2021 17:19:50 +0000 Subject: add a length $D to this to make sure we get enough of the repeat --- challenge-139/james-smith/perl/ch-2.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/challenge-139/james-smith/perl/ch-2.pl b/challenge-139/james-smith/perl/ch-2.pl index cd0c2c56ca..c828aefea7 100644 --- a/challenge-139/james-smith/perl/ch-2.pl +++ b/challenge-139/james-smith/perl/ch-2.pl @@ -16,6 +16,6 @@ say $_ for @long_primes; sub rec_len { my( $D, $N, $s ) = ( shift, 1, '' ); - $s.=int($N/$D),$N%=$D,$N.=0 for 0..2*$D; + $s.=int($N/$D),$N%=$D,$N.=0 for 0..2*$D+length $D; $s =~ /(.+?)\1+$/ ? length $1 : 0; } -- cgit From 913810214acef4eaeb99f1ccb16c4f03ebd1bd61 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Thu, 18 Nov 2021 17:25:38 +0000 Subject: added some notes --- challenge-139/james-smith/perl/ch-2.pl | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) 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; -- cgit