aboutsummaryrefslogtreecommitdiff
path: root/challenge-077/andinus/README.org
blob: 0dfbb7bf1a50e11eb0251938de07079bbb06e104 (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
#+SETUPFILE: ~/.emacs.d/org-templates/level-2.org
#+HTML_LINK_UP: ../index.html
#+OPTIONS: toc:2
#+EXPORT_FILE_NAME: index
#+TITLE: Challenge 077

* 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
- Program: [[file:perl/ch-1.pl]]

Make a list of all possible sums of =$input=.
#+BEGIN_SRC perl
my @sums;
foreach my $num (0 ... $input / 2) {
    my $diff = $input - $num;
    push @sums, [$diff, $num];
}
#+END_SRC

Loop over =@sums= & then print those sets which have both =$sums->[0]= &
=$sums->[1]= in fibonacci series.
#+BEGIN_SRC perl
sub is_fib { return Math::Fibonacci::isfibonacci(@_) }

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