Suma de la serie 1 / 1 + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + – – – – – – hasta n términos.

Dado un entero positivo n, encuentre la suma de la serie hasta n términos.
Ejemplos: 
 

Input : n = 4 
Output : 3.91667
Series : (1 / 1) + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3) + (1 + 2 + 3 + 4) / (1 * 2 * 3 * 4)
       = 1 / 1 + 3 / 2 + 6 / 6 + 10 / 24
       = 3.91667

Input : n = 6
Output : 4.07083
Series : (1 / 1) + (1 + 2) / (1 * 2) + (1 + 2 + 3) / (1 * 2 * 3)  + (1 + 2 + 3 + 4) / (1 * 2 * 3 * 4) + (1 + 2 + 3 + 4 + 5) / (1 * 2 * 3 * 4 * 5)  + (1 + 2 + 3 + 4 + 5 + 6) / (1 * 2 * 3 * 4 * 5 * 6)
       = 1 / 1 + 3 / 2 + 6 / 6 + 10 / 24 + 15 / 120 + 21 / 720
       = 4.07083

C++

// CPP program to find sum of series.
#include <bits/stdc++.h>
using namespace std;
 
double sumOfSeries(int n)
{
    double res = 0.0 ;
    int sum = 0, prod = 1;
    for (int i = 1 ; i <= n ; i++)
    {
        sum += i;
        prod *= i;
        res += ((double)sum / prod);
    }
    return res;
}
 
// Driver Code
int main()
{
    int n = 4 ;
    cout << sumOfSeries(n) ;   
    return 0;
}

Java

// Java program to find sum of series.
class GFG
{
static double sumOfSeries(int n)
{
    double res = 0.0;
    int sum = 0, prod = 1;
    for (int i = 1; i <= n; i++) {
    sum += i;
    prod *= i;
    res += ((double)sum / prod);
    }
    return res;
}
 
// Driver code
public static void main(String arg[]) {
    int n = 4;
    System.out.println(sumOfSeries(n));
}
}
 
// This code is contributed by Anant Agarwal.

Python3

# Python program to
# find sum of series.
 
def sumOfSeries(n) :
 
    res = 0.0
    sum = 0
    prod = 1
    for i in range(1, n + 1) :
     
        sum = sum + i
        prod = prod * i
        res = res + (sum / prod)
         
    return res
     
# Driver Code
n = 4
print (round(sumOfSeries(n), 5))
 
# This code is contributed by
# Manish Shaw(manishshaw1)

C#

// C# program to find sum of series.
using System;
 
class GFG {
 
    static double sumOfSeries(int n)
    {
        double res = 0.0;
        int sum = 0, prod = 1;
        for (int i = 1; i <= n; i++) {
            sum += i;
            prod *= i;
            res += ((double)sum / prod);
        }
        return res;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 4;
        Console.Write(sumOfSeries(n));
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program to find sum of series.
 
function sumOfSeries($n)
{
    $res = 0.0 ;
    $sum = 0; $prod = 1;
    for ($i = 1 ; $i <= $n ; $i++)
    {
        $sum += $i;
        $prod *= $i;
        $res += ((double)$sum / $prod);
    }
    return $res;
}
 
// Driver Code
$n = 4 ;
echo(sumOfSeries($n)) ;
 
// This code is contributed by Ajit.
?>

Javascript

<script>
 
// javascript program to find sum of series.
 
function sumOfSeries(n)
{
    let res = 0.0 ;
    let sum = 0, prod = 1;
    for (let i = 1 ; i <= n ; i++)
    {
        sum += i;
        prod *= i;
        res += (sum / prod);
    }
    return res;
}
 
// Driver Code
let n = 4 ;
   document.write(sumOfSeries(n).toFixed(5)) ; 
    
// This code contributed by aashish1995
 
</script>

Producción : 
 

3.91667

Complejidad de tiempo : O (n) desde que se usa un solo ciclo

Espacio auxiliar : O(1) para espacio constante para variables
 

Publicación traducida automáticamente

Artículo escrito por nikunj_agarwal y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *