Javatpoint Logo
Javatpoint Logo

Iccanobif Numbers in Java

Iccanobif numbers are similar to the Fibonacci numbers. Similar to Fibonacci numbers, the current number Iccanobif series is dependent on the previous two Iccanobif numbers. However, the major difference is that unlike, Fibonacci numbers, one has to first reverse the last two numbers and then add it to get current Iccanobif numbers. Note that the word "Iccanobif" is the reverse of the word "Fibonacci". In Iccanobif numbers also the first two numbers are defined as 0 & 1.

Thus,

I(0) = 0; & I(1) = 1; I(2) = I(1) + I(0) = 1 + 0 = 1; I(3) = I(2) + I(1) = 1 + 1 = 2

I(4) = I(3) + I(2) = 2 + 1 = 3; I(5) = I(4) + I(3) = 3 + 2 = 5; I(6) = I(5) + I(4) = 5 + 3 = 8;

I(7) = I(6) + I(5) = 8 + 5 = 13;

Up to this point, the Iccanobif numbers are exactly the same as Fibonacci numbers. From this point onwards, we will see different numbers as compared to Fibonacci numbers.

I(8) = rev(I(7)) + I(6) = rev(13) + 8 = 31 + 8 = 39.

I(9) = rev(I(8)) + rev(I(7)) = rev(39) + rev(13) = 93 + 31 = 124

The rev() method reverses the digits of the numbers. We did not do the reversal of the digits of the number till I(7). It is because up to I(7), all numbers are single digits numbers, and when we reverse a single-digit number, we get the same number. For example rev(3) = 3. Since reversing the digit has no impact on the numbers till I(7), the numbers are the same as the Fibonacci numbers.

Thus, the Iccanobif numbers series is:

0, 1, 1, 2, 3, 5, 8, 13, 39, 124, ...

From 39, the Iccanobif numbers differ from the Fibonacci numbers. That's why we have highlighted the number 39.

Let's see the different approaches to implement the Iccanobif Numbers.

Recursive Approach

Let's see how one can use recursion to find the Iccanobif numbers.

FileName: IccanoBifNumbers.java

Output:

The first 10 Iccanobif Numbers are: 
0 1 1 2 3 5 8 13 39 124

Time Complexity: In the above program, every recursive call leads to two more recursive calls. Thus, the time complexity for computing each Iccanobif number is exponential, which is (2n), where n represents the parameter num of the method findIccanobifNum().

The above program is consuming a lot of time to find the Iccanobif numbers. Thus, we need to optimize it further to reduce the time complexity. The following approach does the same.

Iterative Approach

The above program is consuming a lot of time to find the Iccanobif numbers. Therefore, we need to optimize it further to reduce the time complexity.

FileName: IccanobifNumbers1.java

Output:

The first 10 Iccanobif Numbers are: 
0 1 1 2 3 5 8 13 39 124

Complexity Analysis: In the above program, we have computed the Iccanobif numbers in O(n) time. However, we are also using an extra array for storing the results that leads to the space complexity of O(n), where n is the total number of Iccanobif numbers that needs to be computed.

In terms of space complexity, we can even do some more optimization. If we look carefully, we find that we only need two variables: one for storing the second last Iccanobif number and the other for storing the last Iccanobif number. Thus, we have avoided the usage of an array. See the following program.

FileName: IccanobifNumbers2.java

Output:

The first 10 Iccanobif Numbers are: 
0 1 1 2 3 5 8 13 39 124

Complexity Analysis: Here, we have reduced the space complexity from O(n) to O(1), time complexity still remains the same.

Note: While doing the complexity analysis, we have assumed that the reversing digits of a number take O(1), i.e., constant time. For reversing the digits, one may take the help of the reverse() method of the StringBuilder class. However, before doing that, it is required to convert the numbers into Strings.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA