Program to find first N Iccanobif Numbers
Last Updated :
12 Apr, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int reverseDigits( int num)
{
int rev_num = 0;
while (num > 0) {
rev_num = rev_num * 10 + num % 10;
num = num / 10;
}
return rev_num;
}
void icanobifNumbers( int N)
{
int first = 0, second = 1;
if (N == 1)
cout << first;
else if (N == 2)
cout << first << " " << second;
else {
cout << first << " " << second << " " ;
for ( int i = 3; i <= N; i++) {
int x = reverseDigits(first);
int y = reverseDigits(second);
cout << x + y << " " ;
int temp = second;
second = x + y;
first = temp;
}
}
}
int main()
{
int N = 12;
icanobifNumbers(N);
return 0;
}
|
Java
public class GFG{
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;
}
static void icanobifNumbers( int N)
{
int first = 0 , second = 1 ;
if (N == 1 )
System.out.print(first);
else if (N == 2 )
System.out.print(first + " " + second);
else {
System.out.print(first + " " + second + " " );
for ( int i = 3 ; i <= N; i++) {
int x = reverseDigits(first);
int y = reverseDigits(second);
System.out.print(x + y + " " );
int temp = second;
second = x + y;
first = temp;
}
}
}
public static void main(String []args){
int N = 12 ;
icanobifNumbers(N);
}
}
|
Python3
def reversedigit(num):
rev_num = 0
while num > 0 :
rev_num = rev_num * 10 + num % 10
num = num / / 10
return rev_num
def icanobifNumbers(N):
first = 0
second = 1
if N = = 1 :
print (first)
elif N = = 2 :
print (first, second)
else :
print (first, second, end = " " )
for i in range ( 3 , N + 1 ):
x = reversedigit(first)
y = reversedigit(second)
print (x + y, end = " " )
temp = second
second = x + y
first = temp
N = 12
icanobifNumbers(N)
|
C#
using System;
public class GFG{
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;
}
static void icanobifNumbers( int N)
{
int first = 0, second = 1;
if (N == 1)
Console.Write(first);
else if (N == 2)
Console.Write(first + " " + second);
else {
Console.Write(first + " " + second + " " );
for ( int i = 3; i <= N; i++) {
int x = reverseDigits(first);
int y = reverseDigits(second);
Console.Write(x + y + " " );
int temp = second;
second = x + y;
first = temp;
}
}
}
public static void Main(){
int N = 12;
icanobifNumbers(N);
}
}
|
PHP
<?php
function reverseDigits( $num )
{
$rev_num = 0;
while ( $num > 0)
{
$rev_num = ( $rev_num * 10) +
( $num % 10);
$num = (int)( $num / 10);
}
return $rev_num ;
}
function icanobifNumbers( $N )
{
$first = 0;
$second = 1;
if ( $N == 1)
echo $first ;
else if ( $N == 2)
echo $first , " " , $second ;
else
{
echo $first , " " , $second , " " ;
for ( $i = 3; $i <= $N ; $i ++)
{
$x = reverseDigits( $first );
$y = reverseDigits( $second );
echo ( $x + $y ), " " ;
$temp = $second ;
$second = $x + $y ;
$first = $temp ;
}
}
}
$N = 12;
icanobifNumbers( $N );
?>
|
Javascript
<script>
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 icanobifNumbers(N)
{
let first = 0, second = 1;
if (N == 1)
document.write(first);
else if (N == 2)
document.write(first + " " + second);
else {
document.write(first + " " + second + " " );
for (let i = 3; i <= N; i++) {
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>
|
Output:
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.
Share your thoughts in the comments
Please Login to comment...