Dado un número N , la tarea es imprimir los primeros N términos de la serie:
Ejemplos:
Entrada: N = 7
Salida: 1, 9, 17, 33, 49, 73, 97
Entrada: N = 3
Salida: 1, 9, 17
Enfoque: De la serie dada, encuentre la fórmula para el N-ésimo término:
1st term = 1 2nd term = 9 = 2 * 4 + 1 3rd term = 17 = 2 * 9 - 1 4th term = 33 = 2 * 16 + 1 5th term = 49 = 2 * 25 - 1 6th term = 73 = 2 * 36 + 1 . . Nth term = (2 * N2 + (-1)N)
Por lo tanto:
Enésimo término de la serie
*** QuickLaTeX no puede compilar la fórmula: *** Mensaje de error: Error: nada que mostrar, la fórmula está vacía
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:
CPP
// C++ implementation of the above approach #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 // print it for (int i = 1; i <= N; i++) { ith_term = i % 2 == 0 ? 2 * i * i + 1 : 2 * i * i - 1; cout << ith_term << ", "; } } // Driver Code int main() { int N = 7; printSeries(N); return 0; }
Java
// Java implementation of the above approach import java.util.*; class GFG{ // Function to print the series static void printSeries(int N) { int ith_term = 0; // Generate the ith term and // print it for (int i = 1; i <= N; i++) { ith_term = i % 2 == 0 ? 2 * i * i + 1 : 2 * i * i - 1; System.out.print(ith_term+ ", "); } } // Driver Code public static void main(String[] args) { int N = 7; printSeries(N); } } // This code is contributed by PrinciRaj1992
Python3
# Python implementation of the above approach # Function to print series def printSeries(N): ith_term = 0; # Generate the ith term and # print for i in range(1,N+1): ith_term = 0; if(i % 2 == 0): ith_term = 2 * i * i + 1; else: ith_term = 2 * i * i - 1; print(ith_term,end= ", "); # Driver Code if __name__ == '__main__': N = 7; printSeries(N); # This code is contributed by Princi Singh
C#
// C# implementation of the above approach using System; class GFG{ // Function to print the series static void printSeries(int N) { int ith_term = 0; // Generate the ith term and // print it for (int i = 1; i <= N; i++) { ith_term = i % 2 == 0? 2 * i * i + 1: 2 * i * i - 1; Console.Write(ith_term+ ", "); } } // Driver Code public static void Main() { int N = 7; printSeries(N); } } // This code is contributed by AbhiThakur
Javascript
<script> // javascript implementation of the above approach // Function to print the series function printSeries( N) { let ith_term = 0; // Generate the ith term and // print it for (let i = 1; i <= N; i++) { ith_term = i % 2 == 0 ? 2 * i * i + 1 : 2 * i * i - 1; document.write( ith_term + ", "); } } // Driver Code let N = 7; printSeries(N); // This code is contributed by gauravrajput1 </script>
Producción:
1, 9, 17, 33, 49, 73, 97,
Complejidad de tiempo: O(N)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por shivanisinghss2110 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA