diff options
| -rwxr-xr-x | challenge-090/alexander-pankoff/perl/ch-2.pl | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/challenge-090/alexander-pankoff/perl/ch-2.pl b/challenge-090/alexander-pankoff/perl/ch-2.pl index eb588779f8..a66c4feea7 100755 --- a/challenge-090/alexander-pankoff/perl/ch-2.pl +++ b/challenge-090/alexander-pankoff/perl/ch-2.pl @@ -10,6 +10,8 @@ use List::Util qw(sum0); use Test::Simple tests => 20; +use constant VERBOSE => $ENV{VERBOSE} // 0; + { for ( 0 .. 19 ) { my ( $a, $b ) = map { int( rand( 1000 ) ) + 1 } 0 .. 1; @@ -19,7 +21,16 @@ use Test::Simple tests => 20; } sub ethopian_mul ( $a, $b ) { - return sum0( map { $_->[1] } grep { odd( $_->[0] ) } ethopian_mul_chain( $a, $b ) ); + my @chain = ethopian_mul_chain( $a, $b ); + verbose( "halving $a, doubling $b, till $a becomes 1" ); + verbose( $_->[0] . ' & ' . $_->[1] ) for @chain; + + my @filtered = map { $_->[1] } grep { odd( $_->[0] ) } @chain; + verbose( "taking right values where left value is odd" ); + verbose( $_ ) for @filtered; + my $product = sum0( @filtered ); + verbose( "product is $product" ); + return $product; } sub ethopian_mul_chain ( $a, $b ) { |
