aboutsummaryrefslogtreecommitdiff
path: root/challenge-077/bob-lied/perl/ch-1.pl
blob: 71514fddde3cdc052be077d2f887c64ea0f27d0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env perl
# vim:set ts=4 sw=4 sts=4 et ai wm=0 nu:
#=============================================================================
# ch-1.pl
#=============================================================================
# Copyright (c) 2020, Bob Lied
#=============================================================================
# Perl Weekly Challenge 077 Task #1 > Fibonacci Sum
#=============================================================================
# You are given a positive integer $N.
# Write a script to find out all possible combination of Fibonacci Numbers
# required to get $N on addition.
# You are NOT allowed to repeat a number. Print 0 if none found.

use strict;
use warnings;
use v5.30;

use feature qw/ signatures /;
no warnings qw/ experimental::signatures /;

use lib "lib";
use FibSum qw(_fib);

# The biggest Fibonacci number that fits in an integer is f(93).
# That's way overkill -- only N up to about 3 digits is reasonable.
my $N_MAX = _fib(93);

sub Usage { "Usage: $0 N\n\t0 < N < 10000" };

my $N = shift;

die Usage() unless $N;
die Usage() unless 0 < $N && $N <= $N_MAX;

my $task = FibSum->new($N);
my $result = $task->run();

# Result is an array of arrays.
for my $answer ( @$result )
{
    say join(' + ', sort { $a <=> $b } @$answer), " = $N";
}