diff options
| author | Luca Ferrari <fluca1978@gmail.com> | 2021-10-25 14:28:41 +0200 |
|---|---|---|
| committer | Luca Ferrari <fluca1978@gmail.com> | 2021-10-25 14:28:41 +0200 |
| commit | 2d5b702c9a348f2226fca9c981f0054c485aa1dd (patch) | |
| tree | e943b2d15135ff5cfa86a4850d02713e8a2ce72f | |
| parent | 3fb94d5d2b20c84f2c7ae2299a78dcb1da442131 (diff) | |
| download | perlweeklychallenge-club-2d5b702c9a348f2226fca9c981f0054c485aa1dd.tar.gz perlweeklychallenge-club-2d5b702c9a348f2226fca9c981f0054c485aa1dd.tar.bz2 perlweeklychallenge-club-2d5b702c9a348f2226fca9c981f0054c485aa1dd.zip | |
Another implementation of the second task.
| -rwxr-xr-x | challenge-136/luca-ferrari/raku/ch-2.p6 | 32 |
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; } |
