Dados cuatro números enteros A , B , C y N , donde A , B , C representan los tres primeros números de la serie geekonacci, la tarea es encontrar el término N de la serie geekonacci .
El término N de la serie geekonacci es la suma de sus tres términos anteriores en la serie, es decir, la suma de (N – 1) th , (N – 2) th y (N – 3) th geekonacci números.
Ejemplos:
Entrada: A = 1, B = 3, C = 2, N = 4
Salida: 6
Explicación: La serie geekonacci es 1, 3, 2, 6, 11, 19, ……
Por lo tanto, el cuarto número geekonacci es 6.Entrada: A = 1, B = 3, C = 2, N = 6
Salida: 19
Planteamiento: El problema planteado se puede resolver generando la serie geekonacci hasta N términos e imprimiendo el N -ésimo término de la serie obtenida. Siga los pasos a continuación para resolver este problema:
- Inicialice una array arr[] de tamaño N e inicialice arr[0] = A , arr[1] = B y arr[2] = C .
- Iterar sobre el rango [3, N – 1] y actualizar el valor de cada i -ésimo elemento, es decir, arr[i] como (arr[i – 1] + arr[i – 2] + arr[i – 3]) para obtener el i -ésimo término de la serie geekonacci.
- Después de completar los pasos anteriores, imprima el valor de arr[N – 1] como el número N resultante de la serie geekonacci .
A continuación se muestra la implementación del enfoque anterior:
C++
#include <bits/stdc++.h> using namespace std; // Function to calculate the // N-th Geek-onacci Number int find(int A, int B, int C, int N) { // Stores the geekonacci series int arr[N]; // Store the first three // terms of the series arr[0] = A; arr[1] = B; arr[2] = C; // Iterate over the range [3, N] for (int i = 3; i < N; i++) { // Update the value of arr[i] // as the sum of previous 3 // terms in the series arr[i] = arr[i - 1] + arr[i - 2] + arr[i - 3]; } // Return the last element // of arr[] as the N-th term return arr[N - 1]; } // Driver Code int main() { int A = 1, B = 3, C = 2, N = 4; cout<<(find(A, B, C, N)); return 0; } // This code is contributed by mohit kumar 29.
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to calculate the // N-th Geek-onacci Number static int find(int A, int B, int C, int N) { // Stores the geekonacci series int[] arr = new int[N]; // Store the first three // terms of the series arr[0] = A; arr[1] = B; arr[2] = C; // Iterate over the range [3, N] for (int i = 3; i < N; i++) { // Update the value of arr[i] // as the sum of previous 3 // terms in the series arr[i] = arr[i - 1] + arr[i - 2] + arr[i - 3]; } // Return the last element // of arr[] as the N-th term return arr[N - 1]; } // Driver Code public static void main(String[] args) { int A = 1, B = 3, C = 2, N = 4; System.out.print(find(A, B, C, N)); } }
Python3
# Python3 program for the above approach # Function to calculate the # N-th Geek-onacci Number def find(A, B, C, N) : # Stores the geekonacci series arr = [0] * N # Store the first three # terms of the series arr[0] = A arr[1] = B arr[2] = C # Iterate over the range [3, N] for i in range(3, N): # Update the value of arr[i] # as the sum of previous 3 # terms in the series arr[i] = (arr[i - 1] + arr[i - 2] + arr[i - 3]) # Return the last element # of arr[] as the N-th term return arr[N - 1] # Driver Code A = 1 B = 3 C = 2 N = 4 print(find(A, B, C, N)) # This code is contributed by sanjoy_62.
C#
// C# program for the above approach using System; class GFG{ // Function to calculate the // N-th Geek-onacci Number static int find(int A, int B, int C, int N) { // Stores the geekonacci series int[] arr = new int[N]; // Store the first three // terms of the series arr[0] = A; arr[1] = B; arr[2] = C; // Iterate over the range [3, N] for (int i = 3; i < N; i++) { // Update the value of arr[i] // as the sum of previous 3 // terms in the series arr[i] = arr[i - 1] + arr[i - 2] + arr[i - 3]; } // Return the last element // of arr[] as the N-th term return arr[N - 1]; } // Driver Code public static void Main(string[] args) { int A = 1, B = 3, C = 2, N = 4; Console.Write(find(A, B, C, N)); } } // This code is contributed by code_hunt.
Javascript
<script> // Javascript program for the above approach // Function to calculate the // N-th Geek-onacci Number function find(A, B, C, N) { // Stores the geekonacci series let arr = new Array(N).fill(0); // Store the first three // terms of the series arr[0] = A; arr[1] = B; arr[2] = C; // Iterate over the range [3, N] for (let i = 3; i < N; i++) { // Update the value of arr[i] // as the sum of previous 3 // terms in the series arr[i] = arr[i - 1] + arr[i - 2] + arr[i - 3]; } // Return the last element // of arr[] as the N-th term return arr[N - 1]; } // Driver code let A = 1, B = 3, C = 2, N = 4; document.write(find(A, B, C, N)); </script>
6
Complejidad temporal: O(N)
Espacio auxiliar: O(N)