From 2d5b702c9a348f2226fca9c981f0054c485aa1dd Mon Sep 17 00:00:00 2001 From: Luca Ferrari Date: Mon, 25 Oct 2021 14:28:41 +0200 Subject: Another implementation of the second task. --- challenge-136/luca-ferrari/raku/ch-2.p6 | 32 +++++++++++++++++++------------- 1 file 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; } -- cgit