Dado un número N , la tarea es imprimir los primeros N términos de la siguiente serie:
2 15 41 80 132 197 275 366 470 587…
Ejemplos:
Entrada: N = 7
Salida: 2 15 41 80 132 197 275
Entrada: N = 3
Salida: 2 15 41
Enfoque: De la serie dada podemos encontrar la fórmula para el N-ésimo término:
1er término = 2
2do término = 15 = 13 * 1 + 2
3er término = 41 = 13 * 2 + 15 = 13 * 3 + 2
4to término = 80 = 13 * 3 + 41 = 13 * 6 + 2
5to término = 132 = 13 * 4 + 80 = 13 * 10 + 2
.
.
N-ésimo término = (13 * N * (N – 1)) / 2 + 2
Por lo tanto:
Enésimo término de la serie
Luego itere sobre los números en el rango [1, N] para encontrar todos los términos usando la fórmula anterior e imprímalos.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation to print the // given with the given Nth term #include "bits/stdc++.h" using namespace std; // Function to print the series void printSeries(int N) { int ith_term = 0; // Generate the ith term and for (int i = 1; i <= N; i++) { ith_term = (13 * i * (i - 1)) / 2 + 2; cout << ith_term << ", "; } } // Driver Code int main() { int N = 7; printSeries(N); return 0; }
Java
// Java implementation to print the // given with the given Nth term import java.util.*; class GFG{ // Function to print the series static void printSeries(int N) { int ith_term = 0; // Generate the ith term and for(int i = 1; i <= N; i++) { ith_term = (13 * i * (i - 1)) / 2 + 2; System.out.print(ith_term + ", "); } } // Driver Code public static void main(String[] args) { int N = 7; printSeries(N); } } // This code is contributed by Rajput-Ji
Python3
# Python3 implementation to print the # given with the given Nth term # Function to print the series def printSeries(N): ith_term = 0; # Generate the ith term and for i in range(1, N + 1): ith_term = (13 * i * (i - 1)) / 2 + 2; print(int(ith_term), ", ", end = ""); # Driver Code if __name__ == '__main__': N = 7; printSeries(N); # This code is contributed by amal kumar choubey
C#
// C# implementation to print the // given with the given Nth term using System; class GFG{ // Function to print the series static void printSeries(int N) { int ith_term = 0; // Generate the ith term and for(int i = 1; i <= N; i++) { ith_term = (13 * i * (i - 1)) / 2 + 2; Console.Write(ith_term + ", "); } } // Driver Code public static void Main(String[] args) { int N = 7; printSeries(N); } } // This code is contributed by Rajput-Ji
Javascript
<script> // javascript implementation to print the // given with the given Nth term // Function to print the series function printSeries( N) { let ith_term = 0; // Generate the ith term and for (let i = 1; i <= N; i++) { ith_term = (13 * i * (i - 1)) / 2 + 2; document.write( ith_term + ", "); } } // Driver Code let N = 7; printSeries(N); // This code is contributed by gauravrajput1 </script>
2, 15, 41, 80, 132, 197, 275,
Publicación traducida automáticamente
Artículo escrito por shubhamsingh84100 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA