×

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 String replace() method

Java String replace() method returns new String by replacing old characters with new characters or old CharSequence to new CharSequence. Syntax: public String replace(char oldChar, char newChar) public String replace(CharSequence target, CharSequence replacement) Parameters: oldChar...

2 minutes read.

Practical Number in Java

In this tutorial, we will understand what is meant by practical numbers. We will understand it throughthe aid of examples and implementation in a java programming language. The practical numbers...

5 minutes read.

Second Smallest Number in an Array in Java

By sorting the arrays and returning the second element, we can use Java to discover the second-smallest number in the array. Input:  arr[] = {10, 11, 13, 15, 34, 51} Output: The...

6 minutes read.

Dutch National Flag Problem in Java

Dutch National Flag (DNF) is a programming issue that Edsger Dijkstra put up. The white, red, and blue hues make up the Dutch flag. The goal is to haphazardly set...

6 minutes read.

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

Java 8 Consumer Interface in Java

The Consumer Interface is used to implement the functional programming in Java. The Consumer Interface indicates a function that accepts a single input and outputs a result. These functions don’t...

2 minutes read.

Alice and Bob Problem Java

Alice and Bob were two friends who liked to play games. Both together found a game, which has the description below. The game begins with an integer num, used to create...

2 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.

Java Break Keyword

Break: The word Break is a keyword or a statement in a java programming language.This Keyword break is used to stop the execution of the loop or switch statements etc.The loop...

3 minutes read.

Java Integer hashCode() method

The hashCode()  method of Java Integer class returns a hash code for this Integer.  Syntax public int hashCode() public static int hashCode(int value)  Parameters The parameter ‘value’ represents a value whose hash code...

1 minute read.

Java Do While Loop

When we wish to test the exit condition at the end of the loop, we use a do-while loop. The do-while loop always executes its body at least once, because...

1 minute read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

How to Set Environment Variables for Java

Introduction Java is an object-oriented programming language that is based on classes and can be employed mostly to develop web and desktop applications. No matter the computer architecture, Java applications are...

7 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.

Monsoon Umbrella Problem in Java

The Monsoon Umbrella problem is a classic Java programming problem used to test the skills of a programmer. The problem involves writing a program to determine the number of umbrellas...

3 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 minutes read.

Longest Odd Even Subsequence in Java

In order to solve the Java problem known as the longest odd-even subsequence, one must identify a sequences in a non-negative array having size s that alternately includes odd and...

6 minutes read.