UP / HOME

Challenge 077

Table of Contents

Task 1 - Fibonacci Sum

You are given a positive integer $N.

Write a script to find the total number of Fibonacci Numbers required to get $N on addition. You are NOT allowed to repeat a number. Print 0 if none found.

Note: This solution is incomplete. Others have pushed complete solutions, look at those.

Perl

Make a list of all possible sums of $input.

my @sums;
foreach my $num (0 ... $input / 2) {
    my $diff = $input - $num;
    push @sums, [$diff, $num];
}

Loop over @sums & then print those sets which have both $sums->[0] & $sums->[1] in fibonacci series.

sub is_fib { return Math::Fibonacci::isfibonacci(@_) }

foreach (@sums) {
    next unless is_fib($_->[0]) and is_fib($_->[1]);
    say "$_->[0] + $_->[1]";
}

Andinus / / Modified: 2020-10-20 Tue 18:22 Emacs 27.1 (Org mode 9.4)