aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuca Ferrari <fluca1978@gmail.com>2021-10-25 14:28:41 +0200
committerLuca Ferrari <fluca1978@gmail.com>2021-10-25 14:28:41 +0200
commit2d5b702c9a348f2226fca9c981f0054c485aa1dd (patch)
treee943b2d15135ff5cfa86a4850d02713e8a2ce72f
parent3fb94d5d2b20c84f2c7ae2299a78dcb1da442131 (diff)
downloadperlweeklychallenge-club-2d5b702c9a348f2226fca9c981f0054c485aa1dd.tar.gz
perlweeklychallenge-club-2d5b702c9a348f2226fca9c981f0054c485aa1dd.tar.bz2
perlweeklychallenge-club-2d5b702c9a348f2226fca9c981f0054c485aa1dd.zip
Another implementation of the second task.
-rwxr-xr-xchallenge-136/luca-ferrari/raku/ch-2.p632
1 files changed, 19 insertions, 13 deletions
diff --git a/challenge-136/luca-ferrari/raku/ch-2.p6 b/challenge-136/luca-ferrari/raku/ch-2.p6
index 83a5227a9d..6a606785ee 100755
--- a/challenge-136/luca-ferrari/raku/ch-2.p6
+++ b/challenge-136/luca-ferrari/raku/ch-2.p6
@@ -1,22 +1,28 @@
#!raku
sub MAIN( Int $n where { $n > 1 } ) {
- my @fibonacci;
- @fibonacci.push: 1, 1;
- my %solutions;
+ # my @fibonacci;
+ # @fibonacci.push: 1, 1;
+ # my %solutions;
- # compute a reduced fibonacci sequence up to the sum of the number given
- @fibonacci.push: @fibonacci[ * - 1 ] + @fibonacci[ * - 2 ] while $n > ( @fibonacci[ * - 1] + @fibonacci[ * - 2 ]);
+ # # compute a reduced fibonacci sequence up to the sum of the number given
+ # @fibonacci.push: @fibonacci[ * - 1 ] + @fibonacci[ * - 2 ] while $n > ( @fibonacci[ * - 1] + @fibonacci[ * - 2 ]);
- # iterate over all the available numbers, and compute the sum
- # and if the sum does match, add the array to the hash of solutions
- # with a stringified key representation
- %solutions{ $_.join( ' + ') } = $_ if ( ( [+] $_ ) == $n ) for @fibonacci.combinations.unique;
+ # # iterate over all the available numbers, and compute the sum
+ # # and if the sum does match, add the array to the hash of solutions
+ # # with a stringified key representation
+ # %solutions{ $_.join( ' + ') } = $_ if ( ( [+] $_ ) == $n ) for @fibonacci.combinations.unique;
- # print the number of keys
- say %solutions.keys.elems;
- # and print all the sums
- .join( " + " ).say for %solutions.values;
+ # # print the number of keys
+ # say %solutions.keys.elems;
+ # # and print all the sums
+ # .join( " + " ).say for %solutions.values;
+
+
+ my @fibonacci = 1, 1, * + * ... * > $n;
+ my @solutions = @fibonacci.unique.combinations.grep( *.sum == $n );
+ @solutions.elems.say;
+ .join( ' + ' ).say for @solutions;
}