Dado un número N , la tarea es imprimir los primeros N términos de la serie 6, 28, 66, 120, 190, 276, etc.
Ejemplos:
Entrada: N = 10
Salida: 6 28 66 120 190 276 378 496 630 780
Entrada: N = 4
Salida: 6 28 66 120
Enfoque: Para resolver el problema mencionado anteriormente, debemos observar el siguiente patrón:
La fórmula general está dada por:
k * (2 * k – 1) , donde inicialmente, k = 2
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to print the series void printSeries(int n) { // Initialise the value of k with 2 int k = 2; // Iterate from 1 to n for (int i = 0; i < n; i++) { // Print each number cout << (k * (2 * k - 1)) << " "; // Increment the value of // K by 2 for next number k += 2; } cout << endl; } // Driver Code int main() { // Given number N int N = 12; // Function Call printSeries(N); return 0; }
Java
// Java program for the above approach class GFG{ // Function to print the series static void printSeries(int n) { // Initialise the value of k with 2 int k = 2; // Iterate from 1 to n for (int i = 0; i < n; i++) { // Print each number System.out.print(k * (2 * k - 1) + " "); // Increment the value of // K by 2 for next number k += 2; } System.out.println(); } // Driver code public static void main(String args[]) { // Given number N int N = 12; // Function Call printSeries(N); } } // This code is contributed by shivaniisnghss2110
Python3
# Python3 program for the above approach # Function to print the series def PrintSeries(n): # Initialise the value of k with 2 k = 2 # Iterate from 1 to n for i in range(0, n): # Print each number print(k * (2 * k - 1), end = ' ') # Increment the value of # K by 2 for next number k = k + 2 # Driver code # Given number n = 12 # Function Call PrintSeries(n) # This code is contributed by poulami21ghosh
C#
// C# program for the above approach using System; class GFG{ // Function to print the series static void printSeries(int n) { // Initialise the value of k with 2 int k = 2; // Iterate from 1 to n for(int i = 0; i < n; i++) { // Print each number Console.Write(k * (2 * k - 1) + " "); // Increment the value of // K by 2 for next number k += 2; } Console.WriteLine(); } // Driver code public static void Main() { // Given number N int N = 12; // Function call printSeries(N); } } // This code is contributed by sanjoy_62
Javascript
<script> // javascript program for the above approach // Function to print the series function printSeries( n) { // Initialise the value of k with 2 let k = 2; // Iterate from 1 to n for (let i = 0; i < n; i++) { // Print each number document.write((k * (2 * k - 1)) + " "); // Increment the value of // K by 2 for next number k += 2; } document.writeln("<br/>"); } // Driver Code // Given number N let N = 12; // Function Call printSeries(N); // This code is contributed by Rajput-Ji </script>
Producción:
6 28 66 120 190 276 378 496 630 780 946 1128
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por bytecode_20 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA