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: 2022-10-04 Tue 21:34 Emacs 27.2 (Org mode 9.4.4)