×

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.


Related Topics

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

Java Enum

Enumerations are used in programming languages to represent collections of named constants. For instance, the four suits in a deck of playing cards might represent four iterators named Club, Diamond,...

3 minutes read.

How to Create an API in Java?

Introduction The API can be abbreviated as Application Programming Interface. An API is a combination of set of classes and interfaces. It is also equivalent to a simple java program. To...

12 minutes read.

The Maximum Rectangular Area in a Histogram in Java

Continuous bars should be used to form the largest possible rectangle. We'll assume in the interest of convenience that each bar's width is 1. Naive Approach In this method, each bar will be...

6 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.

Java Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.

Package naming convention in Java

It is conceivable that many programmers will use the same name for various types, given that Java programmers from all over the world create classes and interfaces. For illustration, suppose...

4 minutes read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

flour pack problem in Java

Create a method called canPack and give it three int parameters: bigCount, smallCount, and objective. The bigCount option indicates the number of large flour bags (5 kilos each). The smallCount argument indicates...

3 minutes read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Java Single Linkedlist

Like arrays, linked lists are a type of linear data structure. Unlike arrays, which store each element in the same location, linked lists employ pointers to connect the elements together. In...

25 minutes read.

Java Final Keyword

In Java, the last keyword is used to limit the user. The applications of the java final keyword have large range of usage in program development. Last can be: variablemethodclass A final...

3 minutes read.

How to Convert Date to String in Java

How to Convert Date to String in Java We need to convert Date to String in Java may for displaying purpose. We can convert Date to String in Java using the format() method of java.text.DateFormat class. There...

2 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

Difference between print() and println() in Java

In this tutorial, we will discuss the differences between print and println in Java language. There are mainly two methods to display the text on the console printprintln The above two methods are...

4 minutes read.

Java exception list

Java uses exceptions, like the majority of contemporary programming languages, to deal with both errors and "extraordinary events." When an exception arises inside the program, it messes up the regular...

6 minutes read.

Java Linters

When it comes to programming, everyone makes mistakes. Errors are bad for developers since they are difficult to handle. But handling as many as possible errors will bring out the...

6 minutes read.