In this tutorial, we will explore the process of identifying the initial N Iccanobif numbers using the C++ programming language. Prior to delving into the practical application of this concept, it is essential to have a comprehensive understanding of Iccanobif numbers in C++.
What is the Iccanobif Numbers in C++?
Iccanobif numbers share similarities with Fibonacci numbers. Similar to Fibonacci numbers, Iccanobif numbers rely on the preceding two numbers in the sequence to calculate the current number. To obtain the next number in the Iccanobif sequence, we reverse the positions of the last two numbers and then sum them. Interestingly, "Iccanobif" is simply "Fibonacci" spelled in reverse. Furthermore, the initial values for the first two numbers in the Iccanobif sequence are set to 0 and 1.
For Example:
- IN(0) = 0;
- IN(1)= 1;
- IN(2)= IN(1)+ IN(0) = 1+ 0= 1;
- IN(3)= IN(2)+ IN(1)= 1+ 1= 2;
- IN(4)= IN(3)+ IN(2)= 2+ 1= 3;
- IN(5)= IN(4)+ IN(3)= 3+ 2= 5;
- IN(6)= IN(5)+ IN(4)= 5+ 3= 8;
- IN(7)= IN(6)+ IN(5)= 8+ 5= 13;
Until this point, the Iccanobif numbers are completely similar to that of Fibonacci numbers. From the nexcpp tutorial onwards, we can see some different numbers as compared to Fibonacci numbers.
- IN(8) = rev(IN(7) )+ IN(6) = rev(13)+8 =31+8 = 39
- IN(9) = rev(IN(8) )+ rev(IN(7)) = rev(39)+ rev(13) = 93+ 31= 124
- IN(10)= rev(IN(9) )+ rev(IN(8)) = rev(124)+ rev(39) = 421+ 93= 514
Here we can utilize the rev function to invert the order of the digits within the numeric values. We refrained from reversing the digits until encountering the IN(7) condition. This precaution was taken because all numbers preceding IN(7) are single-digit figures; reversing single-digit numbers does not result in different numbers. As an illustration, rev(4) equals 5. Given that reversing the digits has no effect on the numbers prior to IN(7), these numbers align with Fibonacci numbers.
Iccanobif Series:
0, 1, 1, 2, 3, 5, 8, 13, 39, 124, 514, ....
From the number 39, we can observe the distinction between the Fibonacci sequence and the Iccanobif sequence.
Example 1: An iterative approach to finding the first 'N' Iccanobif numbers:
#include <iostream>
class IccanobifNum {
public:
int reverse(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
int find(int num, int a[]) {
// Handling the base case
if (num <= 1) {
a[num] = num;
return num;
}
int last = a[num - 1];
int secondLast = a[num - 2];
// Reversing the last Iccanobif Number
int reverse last = reverse(last);
// Reversing the secondLast Iccanobif Number
int reverseSecondLast = reverse(secondLast);
// Adding the last reversed and the second last reversed
a[num] = reverseLast + reverseSecondLast;
return a[num];
}
};
int main() {
IccanobifNumobj;
int n = 11;
int arr[11];
std::cout << "The first " << n << " Iccanobif Numbers are: " << std::endl;
// Finding the first 10 Iccanobif Numbers
for (int i = 0; i < n; i++) {
int findNum = obj.find(i, arr);
std::cout << findNum << " ";
}
return 0;
}
Output:
Time Complexity:
In the aforementioned code, the time complexity calculated is O(n).
Example 2: Recursive approach to find the first 'N' Iccanobif numbers:
#include <iostream>
class IccanobifNum {
public:
// A method that reverses the digits of the number a
int reverse(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
int find(int num) {
// Handling the base case
if (num <= 1) {
return num;
}
int last = find(num - 1);
int secondLast = find(num - 2);
// Reversing the last Iccanobif Number
int reverseLast = reverse(last);
// Reversing the secondLast Iccanobif Number
int reverseSecondLast = reverse(secondLast);
// Adding the last reversed and the second last reversed
return reverseLast + reverseSecondLast;
}
};
int main() {
IccanobifNumbers obj;
int n = 11;
std::cout << "The first " << n << " Iccanobif Numbers are: " << std::endl;
// Finding the first 10 Iccanobif Numbers
for (int i = 0; i < n; i++) {
int findNum = obj.find(i);
std::cout << findNum << " ";
}
return 0;
}
Output:
Time Complexity:
In the program mentioned earlier, each subsequent recursive invocation results in generating over two additional recursive calls. This characteristic attributes to a time complexity of 2^n for each Iccanobif number, with 'n' denoting the 'num' parameter of the find function.