Suma de la Serie 1 + x/1 + x^2/2 + x^3/3 + .. + x^n/n

Este es un programa de series matemáticas donde el usuario debe ingresar el número de términos hasta los cuales se encuentra la suma de la serie. Después de esto, también necesitamos el valor de x, que forma la base de la serie.
Ejemplos: 
 

Input : base = 2, range = 5
Output : 18.07

Input : base = 1, range = 10
Output : 3.93

Método 1 (Simple) Solo necesitamos seguir la serie y poner los valores de la base en x y el rango de valores en n y obtener la suma.
 

C++

// C++ program to find sum of series
// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/n
#include <math.h>
#include <iostream>
#include <boost/format.hpp>
class gfg
{
public :
double sum(int x, int n)
{
    double i, total = 1.0;
    for (i = 1; i <= n; i++)
        total = total +
                (pow(x, i) / i);
    return total;
}
};
// Driver code
int main()
{
    gfg g;
    int x = 2;
    int n = 5;
    //std::cout<<g.sum(x,n);
    std::cout << boost::format("%.2f") % g.sum(x,n);
    return 0;
}

C

// C program to find sum of series
// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/n
#include <math.h>
#include <stdio.h>
 
double sum(int x, int n)
{
    double i, total = 1.0;
    for (i = 1; i <= n; i++)
        total = total +
                (pow(x, i) / i);
    return total;
}
 
// Driver code
int main()
{
    int x = 2;
    int n = 5;
    printf("%.2f", sum(x, n));
    return 0;
}

Java

// Java program to find sum of series
// 1 + 1/x + x^2/2 + x^3/3 + ....+ x^n/n
import static java.lang.Math.pow;
 
class GFG
{
     
// Java code to print the
// sum of the series
static double sum(int x, int n)
{
    double i, total = 1.0;
    for (i = 1; i <= n; i++)
        total = total +
                (Math.pow(x, i) / i);
 
    return total;
}
 
// Driver code
public static void main(String[] args)
{
    int x = 2;
    int n = 5;
    System.out.printf("%.2f", sum(x, n));
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal

Python3

# Python3 code to find sum of series
# 1 + x/1 + x^2/2 + x^3/3 + .. .+ x^n/n
 
def SUM(x, n):
    total = 1
    for i in range(1, n + 1):
        total = total + ((x**i)/i)
    return total
 
# Driver Code
x = 2
n = 5
s = SUM(x, n)
print(round(s, 2))

C#

// C# program to find sum of series
// 1 + 1/x + x^2/2 + x^3/3 + ....+ x^n/n
using System;
 
class GFG
{
 
    // Java code to print the
    // sum of the series
    static float sum(int x, int n)
    {
        double i, total = 1.0;
        for (i = 1; i <= n; i++)
            total = total +
                    (Math.Pow(x, i) / i);
 
        return (float)total;
    }
 
    // Driver code
    public static void Main()
    {
        int x = 2;
        int n = 5;
        Console.WriteLine(sum(x, n));
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program to find sum of series
// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/n
 
// Code to print the sum
// of the series
function sum($x, $n)
{
    $i; $total = 1.0;
    for ($i = 1; $i <= $n; $i++)
        $total = $total +
                 (pow($x, $i) / $i);
    return $total;
}
 
// Driver code
$x = 2;
$n = 5;
echo(sum($x, $n));
 
// This code is contributed by Ajit.
?>

Javascript

<script>
// JavaScript program to find sum of series
// 1 + x/1 + x^2/2 + x^3/3 + ....+ x^n/n
function sum(x, n)
{
    let i, total = 1.0;
    for (i = 1; i <= n; i++)
        total = total +
                (Math.pow(x, i) / i);
    return total;
}
 
// Driver code
    let g;
    let x = 2;
    let n = 5;
    document.write(sum(x, n).toFixed(2));
 
// This code is contributed by Surbhi Tyagi.
</script>

Producción : 
 

18.07

Complejidad de tiempo: O (nlogn)

Espacio Auxiliar: O(1), ya que no se ha tomado ningún espacio extra>

Método 2 (optimizado) Podemos evitar el uso de la función pow() y reutilizar la potencia calculada previamente. 
 

C++

// C++ program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
#include <bits/stdc++.h>
using namespace std;
 
// C++ code to print the sum
// of the series
double sum(int x, int n)
{
    double i, total = 1.0, multi = x;
    for (i = 1; i <= n; i++)
    {
        total = total + multi / i;
        multi = multi * x;
    }
    return total;
}
 
// Driver code
int main()
{
    int x = 2;
    int n = 5;
    cout << fixed << setprecision(2) << sum(x, n);
    return 0;
}
 
// This code is contributed by shubhamsingh10

C

// C program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
#include <math.h>
#include <stdio.h>
 
// C code to print the sum
// of the series
double sum(int x, int n)
{
    double i, total = 1.0, multi = x;
    for (i = 1; i <= n; i++) {
        total = total + multi / i;
        multi = multi * x;
    }
    return total;
}
 
// Driver code
int main()
{
    int x = 2;
    int n = 5;
    printf("%.2f", sum(x, n));
    return 0;
}

Java

// Java program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
 
class GFG
{
 
// Java code to print the sum
// of the given series
static double sum(int x, int n)
{
    double i, total = 1.0, multi = x;
    for (i = 1; i <= n; i++)
    {
        total = total + multi / i;
        multi = multi * x;
    }
    return total;
}
 
// Driver code
public static void main(String[] args)
{
    int x = 2;
    int n = 5;
    System.out.printf("%.2f", sum(x, n));
}
}
 
// This code is contributed by
// Smitha Dinesh Semwal

Python3

# Python 3 program to find sum of series
# 1 + x^2/2 + x^3/3 + ....+ x^n/n
 
# Python 3 code to print the
# sum of the series
def sum(x, n):
 
    total = 1.0
    multi = x
    for i in range(1, n+1):
        total = total + multi / i
        multi = multi * x
     
    return total
 
 
# Driver code
x = 2
n = 5
print(round(sum(x, n), 2))
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
using System;
 
class GFG
{
 
    // Java code to print the sum
    // of the given series
    static float sum(int x, int n)
    {
        double i, total = 1.0, multi = x;
        for (i = 1; i <= n; i++)
        {
            total = total + multi / i;
            multi = multi * x;
        }
        return (float)total;
    }
 
    // Driver code
    public static void Main()
    {
        int x = 2;
        int n = 5;
        Console.WriteLine(sum(x, n));
    }
}
 
// This code is contributed by vt_m.

PHP

<?php
// PHP program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
 
// code to print the sum
// of the series
function sum($x, $n)
{
    $i; $total = 1.0; $multi = $x;
    for ($i = 1; $i <= $n; $i++)
    {
        $total = $total + $multi / $i;
        $multi = $multi * $x;
    }
    return $total;
}
 
// Driver code
$x = 2;
$n = 5;
echo(sum($x, $n));
 
// This code is contributed by Ajit.
?>

Javascript

<script>
 
// Javascript  program to find sum of series
// 1 + x^2/2 + x^3/3 + ....+ x^n/n
 
// JavaScript code to print the sum
// of the series
function sum(x, n)
{
     
    let total = 1.0;
    let multi = x;
    for (let i = 1; i <= n; i++)
    {
        total = total + multi / i;
        multi = multi * x;
    }
    return total;
}
 
// Driver code
let x = 2;
let n = 5;
document.write(sum(x, n).toFixed(2));
 
// This code is contributed by sravan kumar
 
</script>

Producción :  

18.07

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

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por Chinmoy Lenka 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 *