Dado un número n . El problema es encontrar la suma de los primeros n números pares.
Ejemplos:
Input : n = 4 Output : 20 Sum of first 4 even numbers = (2 + 4 + 6 + 8) = 20 Input : n = 20 Output : 420
Enfoque ingenuo: iterar a través de los primeros n números pares y sumarlos.
C++
// C++ implementation to find sum of // first n even numbers #include <bits/stdc++.h> using namespace std; // function to find sum of // first n even numbers int evenSum(int n) { int curr = 2, sum = 0; // sum of first n even numbers for (int i = 1; i <= n; i++) { sum += curr; // next even number curr += 2; } // required sum return sum; } // Driver program to test above int main() { int n = 20; cout << "Sum of first " << n << " Even numbers is: " << evenSum(n); return 0; }
Java
// Java implementation to find sum // of first n even numbers import java.util.*; import java.lang.*; public class GfG{ // function to find sum of // first n even numbers static int evenSum(int n) { int curr = 2, sum = 0; // sum of first n even numbers for (int i = 1; i <= n; i++) { sum += curr; // next even number curr += 2; } // required sum return sum; } // driver function public static void main(String argc[]) { int n = 20; System.out.println("Sum of first " + n + " Even numbers is: " + evenSum(n)); } } // This code is contributed by Prerna Saini
Python3
# Python3 implementation to find sum of # first n even numbers # function to find sum of # first n even numbers def evensum(n): curr = 2 sum = 0 i = 1 # sum of first n even numbers while i <= n: sum += curr # next even number curr += 2 i = i + 1 return sum # Driver Code n = 20 print("sum of first ", n, "even number is: ", evensum(n)) # This article is contributed by rishabh_jain
C#
// C# implementation to find sum // of first n even numbers using System; public class GfG { // function to find sum of // first n even numbers static int evenSum(int n) { int curr = 2, sum = 0; // sum of first n even numbers for (int i = 1; i <= n; i++) { sum += curr; // next even number curr += 2; } // required sum return sum; } // driver function public static void Main() { int n = 20; Console.WriteLine("Sum of first " + n + " Even numbers is: " + evenSum(n)); } } // This code is contributed by vt-m.
PHP
<?php // PHP implementation to find sum of // first n even numbers // function to find sum of // first n even numbers function evenSum($n) { $curr = 2; $sum = 0; // sum of first n even numbers for ($i = 1; $i <= $n; $i++) { $sum += $curr; // next even number $curr += 2; } // required sum return $sum; } // Driver program to test above $n = 20; echo "Sum of first ".$n." Even numbers is: ".evenSum($n); // this code is contributed by mits ?>
Javascript
<script> // JavaScript implementation to find sum of // first n even numbers // function to find sum of // first n even numbers function evenSum(n) { let curr = 2, sum = 0; // sum of first n even numbers for (let i = 1; i <= n; i++) { sum += curr; // next even number curr += 2; } // required sum return sum; } // Driver program to test above let n = 20; document.write("Sum of first " + n + " Even numbers is: " + evenSum(n)); //This code is contributed by Surbhi Tyagi </script>
Sum of first 20 Even numbers is: 420
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Enfoque eficiente: mediante la aplicación de la fórmula que se indica a continuación.
Sum of first n even numbers = n * (n + 1).
Prueba:
Sum of first n terms of an A.P.(Arithmetic Progression) = (n/2) * [2*a + (n-1)*d].....(i) where, a is the first term of the series and d is the difference between the adjacent terms of the series. Here, a = 2, d = 2, applying these values to eq.(i), we get Sum = (n/2) * [2*2 + (n-1)*2] = (n/2) * [4 + 2*n - 2] = (n/2) * (2*n + 2) = n * (n + 1)
C++
// C++ implementation to find sum of // first n even numbers #include <bits/stdc++.h> using namespace std; // function to find sum of // first n even numbers int evenSum(int n) { // required sum return (n * (n + 1)); } // Driver program to test above int main() { int n = 20; cout << "Sum of first " << n << " Even numbers is: " << evenSum(n); return 0; }
Java
// Java implementation to find sum // of first n even numbers import java.util.*; import java.lang.*; public class GfG{ // function to find sum of // first n even numbers static int evenSum(int n) { // required sum return (n * (n + 1)); } // driver function public static void main(String argc[]) { int n = 20; System.out.println("Sum of first " + n + " Even numbers is: " + evenSum(n)); } } // This code is contributed by Prerna Saini
Python3
# Python3 implementation to find # sum of first n even numbers # function to find sum of # first n even numbers def evensum(n): return n * (n + 1) # Driver Code n = 20 print("sum of first", n, "even number is: ", evensum(n)) # This article is contributed by rishabh_jain
C#
// C# implementation to find sum // of first n even numbers' using System; public class GfG { // function to find sum of // first n even numbers static int evenSum(int n) { // required sum return (n * (n + 1)); } // driver function public static void Main() { int n = 20; Console.WriteLine("Sum of first " + n + " Even numbers is: " + evenSum(n)); } } // This code is contributed by vt_m
PHP
<?php // PHP implementation // to find sum of // first n even numbers // function to find sum of // first n even numbers function evenSum($n) { // required sum return ($n * ($n + 1)); } // Driver Code $n = 20; echo "Sum of first " , $n, " Even numbers is: " , evenSum($n); // This code is contributed // by akt_mit ?>
Javascript
<script> // Javascript implementation // to find sum of // first n even numbers // function to find sum of // first n even numbers function evenSum(n) { // required sum return (n * (n + 1)); } // Driver Code let n = 20; document.write("Sum of first " + n + " Even numbers is: " , evenSum(n)); // This code is contributed // by gfgking </script>
Sum of first 20 Even numbers is: 420
Complejidad Temporal: O(1).
Complejidad espacial : O (1) ya que usa variables constantes
Otro método:
En este método, tenemos que calcular el N-ésimo término,
La fórmula para encontrar el término N, Tn = a+(n-1)d, aquí, a= primer término, d= diferencia común, n= número de términos
Y luego tenemos que aplicar la fórmula para encontrar la suma,
la fórmula es, Sn=(N/2) * (a + Tn), aquí a= primer término, Tn= último término, n= número de término
Esta fórmula también se puede aplicar para la suma de números impares, pero la serie debe tener una misma diferencia común.
C++
// C++ implementation to find sum of // first n even numbers #include <bits/stdc++.h> using namespace std; // function to find sum of // first n even numbers int evenSum(int n) { int tn = 2+(n-1)*2; //find Nth Term //calculate a+(n-1)d //first term is = 2 //common difference is 2 //first term and common difference is same all time // required sum return (n/2) * (2 + tn); //calculate (N/2) * (a + Tn) } // Driver program to test above int main() { int n = 20; cout << "Sum of first " << n << " Even numbers is: " << evenSum(n); return 0; } //Contributed by SoumikMondal
Java
// java implementation to find sum of // first n even numbers import java.io.*; import java.util.*; class GFG { // function to find sum of // first n even numbers public static int evenSum(int n) { int tn = 2+(n-1)*2; //find Nth Term //calculate a+(n-1)d //first term is = 2 //common difference is 2 //first term and common difference is same all time // required sum return (n/2) * (2 + tn); //calculate (N/2) * (a + Tn) } // Driver program to test above public static void main(String[] args) { int n = 20; System.out.println("Sum of first "+n+" Even numbers is: "+evenSum(n)); } } //this code is contributed by aditya942003patil
Python3
# python3 implementation to find sum of # first n even numbers # function to find sum of # first n even numbers def evenSum(n) : tn = 2+(n-1)*2; #find Nth Term #calculate a+(n-1)d #first term is = 2 #common difference is 2 #first term and common difference is same all time # required sum return (int)(n/2) * (2 + tn); #calculate (N/2) * (a + Tn) # Driver code if __name__ == "__main__" : n = 20; print("Sum of first", n ,"Even numbers is:", evenSum(n)); # this code is contributed by aditya942003patil
C#
// c# implementation to find sum of // first n even numbers using System; public class GFG { // function to find sum of // first n even numbers static int evenSum(int n) { int tn = 2+(n-1)*2; //find Nth Term //calculate a+(n-1)d //first term is = 2 //common difference is 2 //first term and common difference is same all time // required sum return (n/2) * (2 + tn); //calculate (N/2) * (a + Tn) } // Driver program to test above public static void Main() { int n = 20; // Function call Console.Write("Sum of first "+n+" Even numbers is: "+evenSum(n)); } } // This code is contributed by aditya942003patil
Javascript
<script> // javascript implementation to find sum of // first n even numbers // function to find sum of // first n even numbers function evenSum(n) { var tn = 2+(n-1)*2; //find Nth Term //calculate a+(n-1)d //first term is = 2 //common difference is 2 //first term and common difference is same all time // required sum return (n/2) * (2 + tn); //calculate (N/2) * (a + Tn) // Array to store frequency of each character } // Driver program to test above var n = 20; // Function call document.write("Sum of first "+n+" Even numbers is: "+evenSum(n)); // This code is contributed by aditya942003patil. </script>
Sum of first 20 Even numbers is: 420
Complejidad Temporal: O(1).
Espacio Auxiliar : O(1) ya que usa variables constantes
Publicación traducida automáticamente
Artículo escrito por ayushjauhari14 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA