aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2022-01-04 12:50:48 +0000
committerdrbaggy <js5@sanger.ac.uk>2022-01-04 12:50:48 +0000
commit75bbda873e725dcfea16388f8f443cb5db97787b (patch)
tree8e58c5a8025b536c2f20737d187a3faae5919b21
parentb607a59b4bbe54ef88dd989a2f30a8d33e15368d (diff)
downloadperlweeklychallenge-club-75bbda873e725dcfea16388f8f443cb5db97787b.tar.gz
perlweeklychallenge-club-75bbda873e725dcfea16388f8f443cb5db97787b.tar.bz2
perlweeklychallenge-club-75bbda873e725dcfea16388f8f443cb5db97787b.zip
updated
-rw-r--r--challenge-146/james-smith/README.md25
-rw-r--r--challenge-146/james-smith/perl/ch-1.pl10
-rw-r--r--challenge-146/james-smith/perl/ch-2.pl16
3 files changed, 25 insertions, 26 deletions
diff --git a/challenge-146/james-smith/README.md b/challenge-146/james-smith/README.md
index b25034a695..b1509962ae 100644
--- a/challenge-146/james-smith/README.md
+++ b/challenge-146/james-smith/README.md
@@ -23,16 +23,14 @@ https://github.com/drbaggy/perlweeklychallenge-club/tree/master/challenge-146/ja
We could use a Prime module, but finding primes is not that difficult so we will roll our own generator:
```perl
-my @primes = (3);
-
-for( my $c=5; @primes<10000; $c+=2 ) {
- ($_*$_>$c)?((push@primes,$c),last):$c%$_||last for @primes;
+my($c,@p)=(5,3);
+for(;@p<10000;$c+=2){
+ ($_*$_>$c)?((push@p,$c),last):$c%$_||last for@p;
}
-
-say $primes[-1];
+say$p[-1];
```
-The crux of the code is in the `for @primes` line. This sees if a given odd number is prime.
+The crux of the code is in the `for @` line. This sees if a given odd number is prime.
We loop through all the primes up to and including the square root of the value we are checking.
If we don't find a prime factor by then we push the new value to the primes list, and go on to
@@ -51,18 +49,19 @@ so the last element is the 10,001st prime.
We notice that:
* if you have a top-heavy fraction then the parent has the same denominator, and the new demoninator is the difference between the numerator and denominator.
* otherwise the numerator stays the same and the denominator becomes the difference between the numerator and denominator.
-We repeat this until we get to the top of the tree where both the denominator and numerator are 1.
+We repeat this until we get to the top of the tree where both the denominator and numerator are less than 2. (In the tree is always 1/1) as all tree members have co-prime numerators and denominators. Other values end when the numerator is 0.
+
+The `stringify` function just converts the tree into a single string (list of fractions) so we can test the tree code.
-The stringify function just converts the tree into a single string (list of fractions) so we can test the tree code.
```perl
sub tree {
- my $tree = [[ my($n,$d)=@_ ]];
- push @{$tree}, [($n,$d)=$d>$n?($n,$d-$n):($n-$d,$d)] while $n>1||$d>1;
- $tree;
+ my@tr=[my($n,$d)=@_];
+ push@tr,$d>$n?[$n,$d-=$n]:[$n-=$d,$d]while$n*$d>1;
+ \@tr;
}
sub stringify {
- join q( ), map { "$_->[0]/$_->[1]" } @{$_[0]};
+ "@{[map{join'/',@{$_}}@{$_[0]}]}";
}
```
diff --git a/challenge-146/james-smith/perl/ch-1.pl b/challenge-146/james-smith/perl/ch-1.pl
index 0743994844..62e572997b 100644
--- a/challenge-146/james-smith/perl/ch-1.pl
+++ b/challenge-146/james-smith/perl/ch-1.pl
@@ -5,10 +5,8 @@ use strict;
use warnings;
use feature qw(say);
-my @primes = (3);
-
-for( my $c=5; @primes<10000; $c+=2 ) {
- ($_*$_>$c)?((push@primes,$c),last):$c%$_||last for @primes;
+my($c,@p)=(5,3);
+for(;@p<10000;$c+=2){
+ ($_*$_>$c)?((push@p,$c),last):$c%$_||last for@p;
}
-
-say $primes[-1];
+say$p[-1];
diff --git a/challenge-146/james-smith/perl/ch-2.pl b/challenge-146/james-smith/perl/ch-2.pl
index 04a53ff1b0..9c30a15515 100644
--- a/challenge-146/james-smith/perl/ch-2.pl
+++ b/challenge-146/james-smith/perl/ch-2.pl
@@ -5,11 +5,13 @@ use strict;
use warnings;
use feature qw(say);
use Test::More;
+use Data::Dumper qw(Dumper);;
my @TESTS = (
- [ 3,5, '3/5 3/2 1/2 1/1' ],
- [ 4,3, '4/3 1/3 1/2 1/1' ],
- [ 101,45, '101/45 56/45 11/45 11/34 11/23 11/12 11/1 10/1 9/1 8/1 7/1 6/1 5/1 4/1 3/1 2/1 1/1' ],
+ [ 3, 5, '3/5 3/2 1/2 1/1' ],
+ [ 4, 3, '4/3 1/3 1/2 1/1' ],
+ [ 101, 45, '101/45 56/45 11/45 11/34 11/23 11/12 11/1 10/1 9/1 8/1 7/1 6/1 5/1 4/1 3/1 2/1 1/1' ],
+ [ 6, 4, '6/4 2/4 2/2 0/2' ], ## Not part of tree - but need to see what it does!
);
is( stringify( tree($_->[0],$_->[1]) ), $_->[2] ) foreach @TESTS;
@@ -17,12 +19,12 @@ is( stringify( tree($_->[0],$_->[1]) ), $_->[2] ) foreach @TESTS;
done_testing();
sub tree {
- my $tree = [[ my($n,$d)=@_ ]];
- push @{$tree}, [($n,$d)=$d>$n?($n,$d-$n):($n-$d,$d)] while $n>1||$d>1;
- $tree;
+ my @tr=[my($n,$d)=@_];
+ push@tr,$d>$n?[$n,$d-=$n]:[$n-=$d,$d]while$n*$d>1;
+ \@tr;
}
sub stringify {
- join q( ), map { "$_->[0]/$_->[1]" } @{$_[0]};
+ "@{[map{join'/',@{$_}}@{$_[0]}]}";
}