From 844fa0fe65fa55fc48b7e75c1cd827a07c1315b7 Mon Sep 17 00:00:00 2001 From: drbaggy Date: Mon, 21 Jun 2021 21:13:05 +0100 Subject: add non-recursive version of triangle code - blows up after about n=8 --- challenge-117/james-smith/perl/ch-2.pl | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) 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 -- cgit