Given a number N. The task is to find first N Iccanobif Numbers.
Iccanobif Numbers are similar to Fibonacci Numbers. The K-th Iccanobif number can be obtained by addition of previous two numbers after reversing their digits.
The first few Iccanobif Numbers are:
0, 1, 1, 2, 3, 5, 8, 13, 39, 124, 514, 836, …..
Examples:
Input : N = 5 Output : 0 1 1 2 3 Input : N = 9 Output : 0 1 1 2 3 5 8 13 39 Explanation: Upto 8th term, adding previous two terms is required, as there is an only single digit. For 9th term, adding 31(reversing 8th term) and 8 will give 39.
Approach: The idea is to take the first two Iccanobif Numbers as first = 0 and second = 1. Now iterate using a demoPointer N-2 times and every time find reverse of the previous two numbers using the approach discussed in: Reversing digits of a number. Find the sum of the two reversed numbers and update the variables first and second accordingly.
Below is the implementation of above approach:
// C++ program to find first // N Icanobif numbers #include <bits/stdc++.h> using namespace std;
// Iterative function to // reverse digits of num int reverseDigits( int num)
{ int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
} // Function to print first // N Icanobif Numbers void icanobifNumbers( int N)
{ // Initialize first, second numbers
int first = 0, second = 1;
if (N == 1)
cout << first;
else if (N == 2)
cout << first << " " << second;
else {
// Print first two numbers
cout << first << " " << second << " " ;
for ( int i = 3; i <= N; i++) {
// Reversing digit of previous
// two terms and adding them
int x = reverseDigits(first);
int y = reverseDigits(second);
cout << x + y << " " ;
int temp = second;
second = x + y;
first = temp;
}
}
} // Driver Code int main()
{ int N = 12;
icanobifNumbers(N);
return 0;
} |
// Java program to find first // N Icanobif numbers public class GFG{
// Iterative function to
// reverse digits of num
static int reverseDigits( int num)
{
int rev_num = 0 ;
while (num > 0 ) {
rev_num = rev_num * 10 + num % 10 ;
num = num / 10 ;
}
return rev_num;
}
// Function to print first
// N Icanobif Numbers
static void icanobifNumbers( int N)
{
// Initialize first, second numbers
int first = 0 , second = 1 ;
if (N == 1 )
System.out.print(first);
else if (N == 2 )
System.out.print(first + " " + second);
else {
// Print first two numbers
System.out.print(first + " " + second + " " );
for ( int i = 3 ; i <= N; i++) {
// Reversing digit of previous
// two terms and adding them
int x = reverseDigits(first);
int y = reverseDigits(second);
System.out.print(x + y + " " );
int temp = second;
second = x + y;
first = temp;
}
}
}
// Driver Code
public static void main(String []args){
int N = 12 ;
icanobifNumbers(N);
}
// This code is contributed by ANKITRAI1
} |
# Python 3 program to find first # N Icanobif numbers # Iterative function to # reverse digits of num def reversedigit(num):
rev_num = 0
while num > 0 :
rev_num = rev_num * 10 + num % 10
num = num / / 10
return rev_num
# Function to print first # N Icanobif Numbers def icanobifNumbers(N):
# Initialize first, second numbers
first = 0
second = 1
if N = = 1 :
print (first)
elif N = = 2 :
print (first, second)
else :
# Print first two numbers
print (first, second, end = " " )
for i in range ( 3 , N + 1 ):
# Reversing digit of previous
# two terms and adding them
x = reversedigit(first)
y = reversedigit(second)
print (x + y, end = " " )
temp = second
second = x + y
first = temp
# Driver code N = 12
icanobifNumbers(N) # This code is contributed by Shrikant13 |
// C# program to find first // N Icanobif numbers using System;
public class GFG{
// Iterative function to
// reverse digits of num
static int reverseDigits( int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
// Function to print first
// N Icanobif Numbers
static void icanobifNumbers( int N)
{
// Initialize first, second numbers
int first = 0, second = 1;
if (N == 1)
Console.Write(first);
else if (N == 2)
Console.Write(first + " " + second);
else {
// Print first two numbers
Console.Write(first + " " + second + " " );
for ( int i = 3; i <= N; i++) {
// Reversing digit of previous
// two terms and adding them
int x = reverseDigits(first);
int y = reverseDigits(second);
Console.Write(x + y + " " );
int temp = second;
second = x + y;
first = temp;
}
}
}
// Driver Code
public static void Main(){
int N = 12;
icanobifNumbers(N);
}
} |
<?php // PHP program to find first N // Icanobif numbers // Iterative function to reverse // digits of num function reverseDigits( $num )
{ $rev_num = 0;
while ( $num > 0)
{
$rev_num = ( $rev_num * 10) +
( $num % 10);
$num = (int)( $num / 10);
}
return $rev_num ;
} // Function to print first // N Icanobif Numbers function icanobifNumbers( $N )
{ // Initialize first, second numbers
$first = 0;
$second = 1;
if ( $N == 1)
echo $first ;
else if ( $N == 2)
echo $first , " " , $second ;
else
{
// Print first two numbers
echo $first , " " , $second , " " ;
for ( $i = 3; $i <= $N ; $i ++)
{
// Reversing digit of previous
// two terms and adding them
$x = reverseDigits( $first );
$y = reverseDigits( $second );
echo ( $x + $y ), " " ;
$temp = $second ;
$second = $x + $y ;
$first = $temp ;
}
}
} // Driver Code $N = 12;
icanobifNumbers( $N );
// This code is contributed by Tushil. ?> |
<script> // Javascript program to find first
// N Icanobif numbers
// Iterative function to
// reverse digits of num
function reverseDigits(num)
{
let rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = parseInt(num / 10, 10);
}
return rev_num;
}
// Function to print first
// N Icanobif Numbers
function icanobifNumbers(N)
{
// Initialize first, second numbers
let first = 0, second = 1;
if (N == 1)
document.write(first);
else if (N == 2)
document.write(first + " " + second);
else {
// Print first two numbers
document.write(first + " " + second + " " );
for (let i = 3; i <= N; i++) {
// Reversing digit of previous
// two terms and adding them
let x = reverseDigits(first);
let y = reverseDigits(second);
document.write(x + y + " " );
let temp = second;
second = x + y;
first = temp;
}
}
}
let N = 12;
icanobifNumbers(N);
</script> |
0 1 1 2 3 5 8 13 39 124 514 836
Time Complexity: O(NlogN)
Auxiliary Space: O(1)
Note: For the larger value of N, use numbers as strings.