isFibLike
Category: Arrays
Author: Stuart Reges
Book Chapter: 7.2
Problem: isFibLike
Write a static method called isFibLike that takes an
array of integers as a parameter and that returns whether or not the
sequence matches the pattern of the Fibonacci sequence (true if it does,
false if it does not). The Fibonacci sequence begins with the number 1
followed by the number 1 and each successive value is the sum of the two
previous values: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, and so on.
It is possible to follows this pattern with different starting values. For
example, Lucas numbers start with the values 2 and 1 but otherwise follow
the Fibonacci pattern.
Your method should determine whether each value after the first two is the
sum of the previous two values in the sequence, returning true if the
sequence has that pattern and returning false if it does not. If the array
has two or fewer values, your method should return true. Below are sample
arrays and the value that should be returned for each:
Contents of Array Value returned
passed to isFibLike by isFibLike
--------------------------- --------------
{} true
{42} true
{18, 42} true
{1, 1, 1} false
{1, 2, 3} true
{0, 0, 0, 0, 0} true
{1, 1, 2, 3, 5, 8, 13, 21} true
{2, 1, 3, 4, 7, 11, 18, 29} true
{1, 1, 2, 3, 5, 12, 17} false