aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-06-21 21:13:05 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-06-21 21:13:05 +0100
commit844fa0fe65fa55fc48b7e75c1cd827a07c1315b7 (patch)
tree656893bebc99a460fd47868ddfb76bcd04e8a4d5
parent8627536998136292fdad25ec9a9dc2da281445b0 (diff)
downloadperlweeklychallenge-club-844fa0fe65fa55fc48b7e75c1cd827a07c1315b7.tar.gz
perlweeklychallenge-club-844fa0fe65fa55fc48b7e75c1cd827a07c1315b7.tar.bz2
perlweeklychallenge-club-844fa0fe65fa55fc48b7e75c1cd827a07c1315b7.zip
add non-recursive version of triangle code - blows up after about n=8
-rw-r--r--challenge-117/james-smith/perl/ch-2.pl20
1 files changed, 19 insertions, 1 deletions
diff --git a/challenge-117/james-smith/perl/ch-2.pl b/challenge-117/james-smith/perl/ch-2.pl
index d68f4d9c9b..c4ee3fda2e 100644
--- a/challenge-117/james-smith/perl/ch-2.pl
+++ b/challenge-117/james-smith/perl/ch-2.pl
@@ -48,7 +48,8 @@ my @cache;
## RRR
if( $N < 0 ) { ## Run recursive dumper!
- triangle( -$N, 0, '' );
+ tr_nr( -$N, 0, '' );
+ #triangle( -$N, 0, '' );
exit;
}
@@ -68,6 +69,23 @@ cmpthese( 10000, {
'recrel' => sub { schröder_recurrence_rel( $N ); },
});
+sub tr_nr {
+ my $size = shift;
+ my @line = map { ['H'x$_] } reverse 0 .. $size;
+ while(@line>1) {
+ my @new = ([map( {'L'.$_ } @{$line[-2]}), map {'R'.$_ } @{$line[-1]} ]);
+ while(@line>2) {
+ pop @line;
+ push @new, [
+ map( {'H'.$_ } @{$new[ -1]}),
+ map( {'L'.$_ } @{$line[-2]}),
+ map {'R'.$_ } @{$line[-1]}
+ ];
+ }
+ @line = @new;
+ }
+ say $_ foreach @{$line[0]};
+}
sub triangle {
## As asked display results - note as $n gets large storing in an
## array and returning values is too memory intensive - so we will