Iccanobif Numbers in Java
In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on them.
Iccanobif Numbers
Comparable to Fibonacci numbers are iccanobif numbers. The current number in the Iccanobif series is reliant on the preceding two numbers, much like Fibonacci numbers are. The main distinction is that, unlike Fibonacci numbers, one must first reverse the final two digits before adding them to obtain the current Iccanobif numbers. The word "Iccanobif" is, as you may have noticed, the opposite of the phrase "Fibonacci." The very first two pairs are also specified as 0 and 1 in Iccanobif numerals.
Let us discuss a small example about it.
Input: N = 10
Output: 0 1 1 2 3 5 8 13 39
The 10th term will be the result of the addition of the reverse of the 9th integer and the reverse of 8th integer. Hence the resultant iccanobif number is 124.
There are many ways to find out the sequence of the Iccanobif numbers. They probably are
- User defined Method
- Robust Method
User Defined Method
Let us understand the above approach by a simple example program
File name : Iccanobif.java
public class Iccanobif
{
public int reverseNum(int a)
{
int res = 0;
while(a != 0 )
{
res = res * 10;
res = res + (a % 10);
a = a / 10;
}
return res;
}
// a method that finds Iccanobif numbers
public int findIccanobifNum(int num)
{
// handling the base case
if(num <= 1)
{
return num;
}
// Identifying the final and second-final Iccanobif Numbers
int last = findIccanobifNum(num - 1);
int secondLast = findIccanobifNum(num - 2);
int reverseLast = reverseNum(last);
int reverseSecondLast = reverseNum(secondLast);
return reverseLast + reverseSecondLast;
}
public static void main(String argvs[])
{
// making a component of the IccanobifNumbers class
Iccanobif obj = new Iccanobif();
int n = 12;
System.out.println("The first " + n + " Iccanobif Numbers are: ");
for(int i = 0; i < n; i++)
{
int findNum = obj.findIccanobifNum(i);
System.out.print(findNum + " ");
}
}
}
Output
The first 12 Iccanobif Numbers are:
0 1 1 2 3 5 8 13 39 124 514 836
Every recursive call in the programme mentioned above results in two additional calls. As a result, each Iccanobif number has an exponential time complexity, or (2n), where n is the option num of the technique findIccanobifNum ().
Finding the Iccanobif numbers takes a long time using the aforementioned application. To lessen the time complexity, we must therefore further optimise it.
Robust Method
Let us go through the below program to understand this approach.
File name: Iccanobif1.java
public class Iccanobif1
{
public int reverseNum(int a)
{
int res = 0;
while(a != 0 )
{
res = res * 10;
res = res + (a % 10);
a = a / 10;
}
return res;
}
public int findIccanobifNum(int num, int a[])
{
// addressing the default case
if(num <= 1)
{
a[num] = num;
return num;
}
// obtaining the final and second-final Iccanobif Numbers
int last = a[num - 1];
int secondLast = a[num - 2];
// the final Iccanobif Number reversed
int reverseLast = reverseNum(last);
// the second-to-last Iccanobif number reversed
int reverseSecondLast = reverseNum(secondLast);
// To obtain the current Iccanobif Numbers, combine the most recently reversed and the //second most recently reversed Iccanobif Numbers.
a[num] = reverseLast + reverseSecondLast;
return a[num];
}
public static void main(String argvs[])
{
Iccanobif1 obj = new Iccanobif1();
int n = 12;
int arr[] = new int[10];
System.out.println("The first " + n + " Iccanobif Numbers are: ");
for(int i = 0; i < n; i++)
{
int findNum = obj.findIccanobifNum(i, arr);
System.out.print(findNum + " ");
}
}
}
Output
The first 12 Iccanobif Numbers are:
0 1 1 2 3 5 8 13 39 124 514 836
The Iccanobif numbers were computed in O(n) time in the aforementioned programme. The maximum count of Iccanobif numbers which must be calculated is n, and we are adding an additional array to store the results, which results in a space complexity of O(n).
We can even further optimise in terms of space complexity. We only require two variables, one for needed to store the second-to-last Iccanobif number and another for storing the final Iccanobif number, if we pay close attention. Consequently, we have avoided using an array.