Dado un número N. La tarea es encontrar primero N Números Iccanobif .
Los números de Iccanobif son similares a los números de Fibonacci. El número K-th Iccanobif se puede obtener sumando los dos números anteriores después de invertir sus dígitos.
Los primeros números de Iccanobif son:
0, 1, 1, 2, 3, 5, 8, 13, 39, 124, 514, 836, …..
Ejemplos :
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.
Enfoque: La idea es tomar los primeros dos números de Iccanobif como primero = 0 y segundo = 1 . Ahora itere usando un demoPointer N-2 veces y cada vez encuentre el reverso de los dos números anteriores usando el enfoque discutido en: Inversión de dígitos de un número . Encuentre la suma de los dos números invertidos y actualice las variables primero y segundo según corresponda.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to find first // N Icanobif numbers #include <bits/stdc++.h> using namespace std; // Iterative function to // reverse digits of num int reversDigits(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 = reversDigits(first); int y = reversDigits(second); cout << x + y << " "; int temp = second; second = x + y; first = temp; } } } // Driver Code int main() { int N = 12; icanobifNumbers(N); return 0; }
Java
// Java program to find first // N Icanobif numbers public class GFG{ // Iterative function to // reverse digits of num static int reversDigits(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 = reversDigits(first); int y = reversDigits(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 }
Python3
# 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#
// C# program to find first // N Icanobif numbers using System; public class GFG{ // Iterative function to // reverse digits of num static int reversDigits(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 = reversDigits(first); int y = reversDigits(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 // PHP program to find first N // Icanobif numbers // Iterative function to reverse // digits of num function reversDigits($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 = reversDigits($first); $y = reversDigits($second); echo ($x + $y), " "; $temp = $second; $second = $x + $y; $first = $temp; } } } // Driver Code $N = 12; icanobifNumbers($N); // This code is contributed by Tushil. ?>
Javascript
<script> // Javascript program to find first // N Icanobif numbers // Iterative function to // reverse digits of num function reversDigits(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 = reversDigits(first); let y = reversDigits(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
Complejidad de tiempo: O (NlogN)
Espacio Auxiliar: O(1)
Nota : para el valor mayor de N, use números como strings.