Dado un número N , la tarea es imprimir los primeros N términos de la siguiente serie:
1, 3, 4, 8, 15, 27, 50…
Ejemplos:
Entrada: N = 7
Salida: 1, 3, 4, 8, 15, 27, 50
Entrada: N = 3
Salida: 1, 3, 4
Enfoque: De la serie dada podemos encontrar la fórmula para el N-ésimo término:
1er término = 1, 2do término = 3, 3er término = 4
4to término = 1er término + 2do término + 3er término
5to término = 2do término + 3er término + 4to término
6to término = 3er término + 4to término + 5to término
.
.
pronto
Por lo tanto, la idea es hacer un seguimiento de los últimos tres términos de la serie y encontrar los términos consecutivos de la serie.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to print the // N terms of the series whose three // terms are given #include "bits/stdc++.h" using namespace std; // Function to print the series void printSeries(int n, int a, int b, int c) { int d; // Generate the ith term and // print it if (n == 1) { cout << a << " "; return; } if (n == 2) { cout << a << " " << b << " "; return; } cout << a << " " << b << " " << c << " "; for (int i = 4; i <= n; i++) { d = a + b + c; cout << d << " "; a = b; b = c; c = d; } } // Driver Code int main() { int N = 7, a = 1, b = 3; int c = 4; // Function Call printSeries(N, a, b, c); return 0; }
Java
// Java implementation to print the // N terms of the series whose three // terms are given //include "bits/stdJava.h" import java.util.*; class GFG{ // Function to print the series static void printSeries(int n, int a, int b, int c) { int d; // Generate the ith term and // print it if (n == 1) { System.out.print(a + " "); return; } if (n == 2) { System.out.print(a + " " + b + " "); return; } System.out.print(a + " " + b + " " + c + " "); for (int i = 4; i <= n; i++) { d = a + b + c; System.out.print(d + " "); a = b; b = c; c = d; } } // Driver Code public static void main(String[] args) { int N = 7, a = 1, b = 3; int c = 4; // Function Call printSeries(N, a, b, c); } } // This code is contributed by sapnasingh4991
Python3
# Python3 implementation to print the # N terms of the series whose three # terms are given # Function to print the series def printSeries(n, a, b, c): # Generate the ith term and # print it if (n == 1): print(a, end = " "); return; if (n == 2): print(a, b, end = " "); return; print(a, b, c, end = " "); for i in range (4, n + 1): d = a + b + c; print(d, end = " "); a = b; b = c; c = d; # Driver Code N = 7; a = 1; b = 3; c = 4; # Function Call printSeries(N, a, b, c); # This code is contributed by Code_Mech
C#
// C# implementation to print the // N terms of the series whose three // terms are given using System; class GFG{ // Function to print the series static void printSeries(int n, int a, int b, int c) { int d; // Generate the ith term and // print it if (n == 1) { Console.Write(a + " "); return; } if (n == 2) { Console.Write(a + " " + b + " "); return; } Console.Write(a + " " + b + " " + c + " "); for(int i = 4; i <= n; i++) { d = a + b + c; Console.Write(d + " "); a = b; b = c; c = d; } } // Driver Code public static void Main() { int N = 7, a = 1, b = 3; int c = 4; // Function call printSeries(N, a, b, c); } } // This code is contributed by rock cool
Javascript
<script> // javascript implementation to print the // N terms of the series whose three // terms are given // Function to print the series function printSeries( n, a, b, c) { let d; // Generate the ith term and // print it if (n == 1) { document.write( a + " "); return; } if (n == 2) { document.write( a + " " + b + " "); return; } document.write( a + " " + b + " " + c + " "); for (let i = 4; i <= n; i++) { d = a + b + c; document.write( d + " "); a = b; b = c; c = d; } } // Driver Code let N = 7, a = 1, b = 3; let c = 4; // Function Call printSeries(N, a, b, c); // This code is contributed by gauravrajput1 </script>
1 3 4 8 15 27 50
Publicación traducida automáticamente
Artículo escrito por shubhamsingh84100 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA