Dados dos números enteros N y K , la tarea es encontrar la suma de los primeros N números naturales y luego actualizar N como la suma calculada previamente. Repita estos pasos K veces y finalmente imprima el valor de N.
Ejemplos:
Entrada: N = 2, K = 2
Salida: 6
Operación 1: n = suma(n) = suma(2) = 1 + 2 = 3
Operación 2: n = suma(n) = suma(3) = 1 + 2 + 3 = 6
Entrada: N = 3, K = 2
Salida: 21
Enfoque: Encuentre la suma de los primeros N números naturales usando la fórmula (N * (N + 1)) / 2 y luego actualice N con la suma calculada. Repita estos pasos exactamente K veces e imprima el valor final de N.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to return the sum of // the first n natural numbers int sum(int n) { int sum = (n * (n + 1)) / 2; return sum; } // Function to return the repeated sum int repeatedSum(int n, int k) { // Perform the operation exactly k times for (int i = 0; i < k; i++) { // Update n with the sum of // first n natural numbers n = sum(n); } return n; } // Driver code int main() { int n = 2, k = 2; cout << repeatedSum(n, k); return 0; }
Java
// Java implementation of the approach class GFG { // Function to return the sum of // the first n natural numbers static int sum(int n) { int sum = (n * (n + 1)) / 2; return sum; } // Function to return the repeated sum static int repeatedSum(int n, int k) { // Perform the operation exactly k times for (int i = 0; i < k; i++) { // Update n with the sum of // first n natural numbers n = sum(n); } return n; } // Driver code public static void main (String[] args) { int n = 2, k = 2; System.out.println(repeatedSum(n, k)); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the approach # Function to return the sum of # the first n natural numbers def sum(n): sum = (n * (n + 1)) // 2 return sum # Function to return the repeated sum def repeatedSum(n, k): # Perform the operation exactly k times for i in range(k): # Update n with the sum of # first n natural numbers n = sum(n) return n # Driver code n = 2 k = 2 print(repeatedSum(n, k)) # This code is contributed by Mohit Kumar
C#
// C# implementation of the approach using System; class GFG { // Function to return the sum of // the first n natural numbers static int sum(int n) { int sum = (n * (n + 1)) / 2; return sum; } // Function to return the repeated sum static int repeatedSum(int n, int k) { // Perform the operation exactly k times for (int i = 0; i < k; i++) { // Update n with the sum of // first n natural numbers n = sum(n); } return n; } // Driver code public static void Main (String[] args) { int n = 2, k = 2; Console.WriteLine(repeatedSum(n, k)); } } // This code is contributed by PrinciRaj1992
Javascript
<script> // javascript implementation of the approach // Function to return the sum of // the first n natural numbers function sum(n) { var sum = (n * (n + 1)) / 2; return sum; } // Function to return the repeated sum function repeatedSum(n , k) { // Perform the operation exactly k times for (i = 0; i < k; i++) { // Update n with the sum of // first n natural numbers n = sum(n); } return n; } // Driver code var n = 2, k = 2; document.write(repeatedSum(n, k)); // This code contributed by Rajput-Ji </script>
6
Complejidad de tiempo: O(k)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por ayushgoyal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA