Dados dos valores ‘a’ y ‘n’, encuentre la suma de la serie a^1/1. + a^2/2! + a^3/3! + a^4/4! +…….+ un^n/n!.
Ejemplos:
Input : a = 2 and n = 5 Output : 6.26667 We get result by adding 2^1/1! + 2^2/2! + 2^3/3! + 2^4/4! + 2^5/5! = 2/1 + 4/2 + 8/6 + 16/24 + 32/120 = 6.26667
Una solución simple es calcular uno por uno los valores de los términos individuales y seguir agregándolos al resultado.
Podemos encontrar la solución con un solo bucle. La idea es simplemente usar los valores anteriores y multiplicar por (a/i) donde i es el número del término que necesitamos encontrar.
for finding 1st term:- a/1 for finding 2nd term:- (1st term) * a/2 for finding 3rd term:- (2nd term) * a/3 . . . for finding nth term:- ((n-1)th term) * a/n
Ilustración:
Input: a = 2 and n = 5 By multiplying Each term by 2/i 1st term :- 2/1 = 2 2nd term :- (1st term) * 2/2 =(2)*1 = 2 3rd term :- (2nd term) * 2/3 = 4/3 4th term :- (3rd term) * 2/4 = 2/3 5th term :- (4th term) * 2/5 = 4/15 => 2 + 2 + 4/3 + 2/3 + 4/15 Output: sum = 6.26667
CPP
/*CPP program to print the sum of series */ #include<bits/stdc++.h> using namespace std; /*function to calculate sum of given series*/ double sumOfSeries(double a,double num) { double res = 0,prev=1; for (int i = 1; i <= num; i++) { /*multiply (a/i) to previous term*/ prev *= (a/i); /*store result in res*/ res = res + prev; } return(res); } /* Driver Function */ int main() { double n = 5, a=2; cout << sumOfSeries(a,n); return 0; }
Java
// Java program to print the // sum of series import java.io.*; class GFG { public static void main (String[] args) { double n = 5, a = 2; System.out.println(sumOfSeries(a, n)); } // function to calculate sum of given series static double sumOfSeries(double a,double n) { double res = 0, prev = 1; for (int i = 1; i <= n; i++) { // multiply (a/i) to previous term prev *= (a / i); // store result in res res = res + prev; } return(res); } } // This code is Contributed by Azkia Anam.
Python3
# Python program to print # the sum of series. # function to calculate # sum of given series. from __future__ import division def sumOfSeries(a,num): res = 0 prev=1 for i in range(1, n+1): # multiply (a/i) to # previous term prev *= (a/i) # store result in res res = res + prev return res # Driver code n = 5 a = 2 print(round(sumOfSeries(a,n),4)) # This Code is Contributed # by Azkia Anam.
C#
// C# program to print the // sum of series using System; class GFG { public static void Main () { double n = 5, a = 2; Console.WriteLine(sumOfSeries(a, n)); } // Function to calculate sum of given series static float sumOfSeries(double a, double n) { double res = 0, prev = 1; for (int i = 1; i <= n; i++) { // multiply (a/i) to previous term prev *= (a / i); // store result in res res = res + prev; } return(float)(res); } } // This code is Contributed by vt_m.
PHP
<?php // PHP program to print // the sum of series // Function to calculate // sum of given series function sumOfSeries($a, $num) { $res = 0; $prev = 1; for ($i = 1; $i <= $num; $i++) { // multiply (a/i) to // previous term $prev *= ($a / $i); // store result in res $res = $res + $prev; } return ($res); } // Driver Code $n = 5; $a = 2; echo(sumOfSeries($a, $n)); // This code is contributed by Ajit. ?>
Javascript
<script> /* JavaScript program to print the sum of series */ /*function to calculate sum of given series*/ function sumOfSeries(a, num) { let res = 0, prev = 1; for (let i = 1; i <= num; i++) { /*multiply (a/i) to previous term*/ prev *= (a/i); /*store result in res*/ res = res + prev; } return(res); } /* Driver Function */ let n = 5, a=2; document.write(sumOfSeries(a,n)); // This code is contributed by Surbhi Tyagi. </script>
Producción :
6.26667
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1), ya que no se ha ocupado ningún espacio extra.
Este artículo es una contribución de R_Raj . 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