Una serie con la misma diferencia común se conoce como serie aritmética . El primer término de la serie es a y la diferencia común es d . La serie se ve como a, a + d, a + 2d, a + 3d, . . . La tarea es encontrar la suma de la serie.
Ejemplos:
Input : a = 1 d = 2 n = 4 Output : 16 1 + 3 + 5 + 7 = 16 Input : a = 2.5 d = 1.5 n = 20 Output : 335
Una solución simple para encontrar la suma de series aritméticas.
C++
// CPP Program to find the sum of arithmetic // series. #include<bits/stdc++.h> using namespace std; // Function to find sum of series. float sumOfAP(float a, float d, int n) { float sum = 0; for (int i=0;i<n;i++) { sum = sum + a; a = a + d; } return sum; } // Driver function int main() { int n = 20; float a = 2.5, d = 1.5; cout<<sumOfAP(a, d, n); return 0; }
Java
// JAVA Program to find the sum of // arithmetic series. class GFG{ // Function to find sum of series. static float sumOfAP(float a, float d, int n) { float sum = 0; for (int i = 0; i < n; i++) { sum = sum + a; a = a + d; } return sum; } // Driver function public static void main(String args[]) { int n = 20; float a = 2.5f, d = 1.5f; System.out.println(sumOfAP(a, d, n)); } } /*This code is contributed by Nikita Tiwari.*/
Python
# Python Program to find the sum of # arithmetic series. # Function to find sum of series. def sumOfAP( a, d,n) : sum = 0 i = 0 while i < n : sum = sum + a a = a + d i = i + 1 return sum # Driver function n = 20 a = 2.5 d = 1.5 print (sumOfAP(a, d, n)) # This code is contributed by Nikita Tiwari.
C#
// C# Program to find the sum of // arithmetic series. using System; class GFG { // Function to find sum of series. static float sumOfAP(float a, float d, int n) { float sum = 0; for (int i = 0; i < n; i++) { sum = sum + a; a = a + d; } return sum; } // Driver function public static void Main() { int n = 20; float a = 2.5f, d = 1.5f; Console.Write(sumOfAP(a, d, n)); } } // This code is contributed by parashar.
PHP
<?php // PHP Program to find the sum // of arithmetic series. // Function to find sum of series. function sumOfAP($a, $d, $n) { $sum = 0; for ($i = 0; $i < $n; $i++) { $sum = $sum + $a; $a = $a + $d; } return $sum; } // Driver Code $n = 20; $a = 2.5; $d = 1.5; echo(sumOfAP($a, $d, $n)); // This code is contributed by Ajit. ?>
Javascript
<script> // Javascript Program to find the sum of arithmetic // series. // Function to find sum of series. function sumOfAP(a, d, n) { let sum = 0; for (let i=0;i<n;i++) { sum = sum + a; a = a + d; } return sum; } // Driver function let n = 20; let a = 2.5, d = 1.5; document.write(sumOfAP(a, d, n)); // This code is contributed by Mayank Tyagi </script>
Producción:
335
Complejidad de tiempo: O (n)
Una solución eficiente para encontrar la suma de series aritméticas es usar la siguiente fórmula.
Sum of arithmetic series = ((n / 2) * (2 * a + (n - 1) * d)) Where a - First term d - Common difference n - No of terms
C++
// Efficient solution to find sum of arithmetic series. #include<bits/stdc++.h> using namespace std; float sumOfAP(float a, float d, float n) { float sum = (n / 2) * (2 * a + (n - 1) * d); return sum; } // Driver code int main() { float n = 20; float a = 2.5, d = 1.5; cout<<sumOfAP(a, d, n); return 0; }
Java
// Java Efficient solution to find // sum of arithmetic series. class GFG { static float sumOfAP(float a, float d, float n) { float sum = (n / 2) * (2 * a + (n - 1) * d); return sum; } // Driver code public static void main (String[] args) { float n = 20; float a = 2.5f, d = 1.5f; System.out.print(sumOfAP(a, d, n)); } } // This code is contributed by Anant Agarwal.
Python3
# Python3 Efficient # solution to find sum # of arithmetic series. def sumOfAP(a, d, n): sum = (n / 2) * (2 * a + (n - 1) * d) return sum # Driver code n = 20 a = 2.5 d = 1.5 print(sumOfAP(a, d, n)) # This code is # contributed by sunnysingh
C#
// C# efficient solution to find // sum of arithmetic series. using System; class GFG { static float sumOfAP(float a, float d, float n) { float sum = (n / 2) * (2 * a + (n - 1) * d); return sum; } // Driver code static public void Main () { float n = 20; float a = 2.5f, d = 1.5f; Console.WriteLine(sumOfAP(a, d, n)); } } // This code is contributed by Ajit.
PHP
<?php // Efficient PHP code to find sum // of arithmetic series. // Function to find sum of series. function sumOfAP($a, $d, $n) { $sum = ($n / 2) * (2 * $a + ($n - 1) * $d); return $sum; } // Driver code $n = 20; $a = 2.5; $d = 1.5; echo(sumOfAP($a, $d, $n)); // This code is contributed by Ajit. ?>
Javascript
// Efficient solution to find sum of arithmetic series. function sumOfAP(a, d, n) { let sum = (n / 2) * (2 * a + (n - 1) * d); return sum; } // Driver code let n = 20; let a = 2.5, d = 1.5; document.write(sumOfAP(a, d, n)); // This code is contributed by Ashok
335
Complejidad de tiempo : O(1)
Complejidad del espacio : O(1) ya que usa solo variables constantes
¿Cómo funciona esta fórmula?
Podemos probar la fórmula usando inducción matemática. Podemos ver fácilmente que la fórmula se cumple para n = 1 y n = 2. Sea esto cierto para n = k-1.
Let the formula be true for n = k-1. Sum of first k - 1 elements of geometric series is = (((k-1))/ 2) * (2 * a + (k - 2) * d)) We know k-th term of arithmetic series is = a + (k - 1)*d Sum of first k elements = = Sum of (k-1) numbers + k-th element = (((k-1)/2)*(2*a + (k-2)*d)) + (a + (k-1)*d) = [((k-1)(2a + (k-2)d) + (2a + 2kd - 2d)]/2 = ((k / 2) * (2 * a + (k - 1) * d))
Este artículo es una contribución de Dharmendra kumar . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA