Programa para hallar la suma de la serie 1 + x + x^2+ x^3+ .. + x^n

Dado un entero X , la tarea es imprimir la serie y encontrar la suma de la serie  1 + x^{1} + x^{2} + x^{3} + x^{4} ... x^{N}
Ejemplos: 
 

Entrada: X = 2, N = 5 
Salida: Suma = 31 
1 2 4 8 16
Entrada: X = 1, N = 10 
Salida: Suma = 10 
1 1 1 1 1 1 1 1 1 1 
 

Enfoque: La idea es atravesar la serie y calcular la suma de los N términos de la serie. El N -ésimo término de la serie se puede calcular como: 
 

N th Término = (N-1) th Término * X 
 

A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ implementation to find
// sum of series of
// 1 + x^2 + x^3 + ....+ x^n
 
#include <bits/stdc++.h>
 
using namespace std;
 
// Function to find the sum of
// the series and print N terms
// of the given series
double sum(int x, int n)
{
    double i, total = 1.0, multi = x;
 
    // First Term
    cout << total << " ";
 
    // Loop to print the N terms
    // of the series and find their sum
    for (i = 1; i < n; i++) {
 
        total = total + multi;
        cout << multi << " ";
        multi = multi * x;
    }
 
    cout << "\n";
    return total;
}
 
// Driver code
int main()
{
    int x = 2;
    int n = 5;
    cout << fixed
         << setprecision(2)
         << sum(x, n);
    return 0;
}

C

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

Java

// Java implementation to find
// the sum of series
// 1 + x^2 + x^3 + ....+ x^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;
 
        // First Term
        System.out.print("1 ");
 
        // Loop to print the N terms
        // of the series and compute
        // their sum
        for (i = 1; i < n; i++) {
            total = total + multi;
            System.out.print(multi);
            System.out.print(" ");
            multi = multi * x;
        }
 
        System.out.println();
        return total;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int x = 2;
        int n = 5;
        System.out.printf(
            "%.2f", sum(x, n));
    }
}

Python3

# Python3 program to find sum of
# series of 1 + x^2 + x^3 + ....+ x^n
 
# Function to find the sum of
# the series and print N terms
# of the given series
def sum(x, n):
     
    total = 1.0
    multi = x
 
    # First Term
    print(1, end = " ")
 
    # Loop to print the N terms
    # of the series and find their sum
    for i in range(1, n):
         
        total = total + multi
        print('%.1f' % multi, end = " ")
        multi = multi * x
         
    print('\n')
    return total;
 
# Driver code
x = 2
n = 5
print('%.2f' % sum(x, n))
 
# This code is contributed by Pratik Basu

C#

// C# implementation to find
// the sum of series
// 1 + x^2 + x^3 + ....+ x^n
using System;
class GFG{
 
// C# code to print the sum
// of the given series
static double sum(int x, int n)
{
    double i, total = 1.0, multi = x;
 
    // First Term
    Console.Write("1 ");
 
    // Loop to print the N terms
    // of the series and compute
    // their sum
    for (i = 1; i < n; i++)
    {
        total = total + multi;
        Console.Write(multi);
        Console.Write(" ");
        multi = multi * x;
    }
    Console.WriteLine();
    return total;
}
 
// Driver Code
public static void Main(String[] args)
{
    int x = 2;
    int n = 5;
    Console.Write("{0:F2}", sum(x, n));
}
}
 
// This code is contributed by Rajput-Ji

Javascript

<script>
// JavaScript implementation to find
// sum of series of
// 1 + x^2 + x^3 + ....+ x^n
 
// Function to find the sum of
// the series and print N terms
// of the given series
function sum(x, n)
{
    let i, total = 1.0, multi = x;
 
    // First Term
    document.write(total + " ");
 
    // Loop to print the N terms
    // of the series and find their sum
    for (i = 1; i < n; i++) {
 
        total = total + multi;
        document.write(multi + " ");
        multi = multi * x;
    }
 
    document.write("<br>");
    return total;
}
 
// Driver code
    let x = 2;
    let n = 5;
    document.write(sum(x, n).toFixed(2));
 
// This code is contributed by Surbhi Tyagi.
</script>
Producción: 

1 2.0 4.0 8.0 16.0 
31.00

 

Complejidad de tiempo: O(n)

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

Publicación traducida automáticamente

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