The skeleton source code for this question is in the package
fibonacciTransform
. You have to implement the methodsisPossible_onlyOneDoubling
andisPossible
in the classFibTransform
.
You are given an integer n
and you want to determine if it is possible to transform n
to a Fibonacci number in at most m
steps, where m
is also a given bound. At each step, you can:
- add 1 to the number at hand (addition step),
- or multiply it by 2 (doubling step).
Implement two methods:
-
The first method,
isPossible_onlyOneDoubling
, where you check ifn
can be transformed into a Fibonacci number using at mostm
steps in total, and of these steps at most one can be a doubling step. The doubling does not have to be the first or last step (see the examples). If you complete only this method correctly then you will receive partial credit of 50% for this question. -
The second method,
isPossible
, generalizes the earlier method to permit any number of+ 1
or* 2
operations as long as the total number of operations is no more thanm
.
See the skeleton code for preconditions for these methods. We are only interested in Fibonacci numbers that can be represented using the Java types Integer
or int
.
For this implementation, we will consider the Fibonacci sequence to be 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
n
= 10,m
= 3- One can obtain 21 as follows:
(10 * 2)+1
. - Both
isPossible_onlyOneDoubling(10, 3)
andisPossible(10,3)
should returntrue
.
- One can obtain 21 as follows:
n
= 22,m
= 3- One can obtain 89 as follows:
((22*2)*2)+1
isPossible_onlyOneDoubling(22, 3)
should returnfalse
andisPossible(22, 3)
should returntrue
.
- One can obtain 89 as follows:
n
= 6,m
= 2- We can obtain 8 as:
(6+1)+1
. - Both
isPossible_onlyOneDoubling(6, 2)
andisPossible(6, 2)
should returntrue
.
- We can obtain 8 as:
n
= 8,m
= 10- 8 is already a Fibonacci number. No transformation steps are needed.
- Both
isPossible_onlyOneDoubling(8,10)
andisPossible(8,10)
should returntrue
.
n
= 0,m
= 0- 0 is already a Fibonacci number for our purposes. No transformation steps are needed.
- Both
isPossible_onlyOneDoubling(0, 0)
andisPossible(0, 0)
should returntrue
.
n
= 20,m
= 0- 20 is not a Fibonacci number. We cannot transform it into a Fibonacci number in 0 steps.
- Both
isPossible_onlyOneDoubling(20, 0)
andisPossible(20, 0)
should returnfalse
.
n
= 24,m
= 6.- 24 can be transformed to 55:
((((24+1)+1)+1)*2)+1
. - Both
isPossible_onlyOneDoubling(24, 6)
andisPossible(24, 6)
should returntrue
.
- 24 can be transformed to 55:
n
= 1000,m
= 18.- 1000 can be transformed to 514229 in 18 steps with more than one doubling.
isPossible_onlyOneDoubling(1000, 18)
should returnfalse
andisPossible(1000, 18)
should returntrue
.