Quick actions

cmd+k|ctrl+k

Navigation

Languages

#077 - 1

Snippet info

Language

Raku

Visibility

public

Author

fernando-correa

Created

2020-09-07T12:57:31Z

Updated

2020-09-07T13:00:53Z

multi fsum([]                   , UInt                       ) { Empty }
multi fsum([UInt $first, *@rest], UInt $num where * <  $first) { Empty }
multi fsum([UInt $first, *@rest], UInt $num where * == $first) { [[ $first, ],] }
multi fsum([UInt $first, *@rest], UInt $num where * >  $first) {
    [|fsum(@rest, $num), |fsum(@rest, $num - $first).map: -> @seq { [$first, |@seq] } ]
}

sub fibonacci-sum($sum) {
   my @fib = 1, 2, &[+] ... *;
   my $f = @fib.first: * > $sum;
   fsum @fib[^$f], $sum
}



for ^20 {
   say .sum, " == ", .join(" + ")  for fibonacci-sum $_ 
}
INFO