📜  算术几何序列之和

📅  最后修改于: 2021-04-26 08:58:35             🧑  作者: Mango

在数学中,算术几何序列是几何级数与算术级数的相应项逐项相乘的结果。

是算术几何序列。
给定a(AP的第一项),n(项数),d(公共差),b(GP的第一项),r(GP的公共比率)的值。任务是找到AGP的前n个项的总和。
例子:

Input : First term of AP, a = 1, 
        Common difference of AP, d = 1, 
        First term of GP, b = 2, 
        Common ratio of GP r = 2,
        Number of terms, n = 3
Output : 34
Explanation
Sum = 1*2 + 2*22 + 3*23
    = 2 + 8 + 24
    = 34

算术几何序列的第n个项是算术序列的n个项与几何个数的n个项的乘积。算术几何序列出现在各种应用中,例如概率论中的期望值计算。例如,计算直到成功的预期试验次数。
AGP的第n个项表示为: t n = [a +(n – 1)* d] *(b * r n-1 )
方法1 :(强力)
想法是找到AGP的每个术语并找到总和。
以下是此方法的实现:

C++
// CPP P rogram to find the sum of first n terms.
#include
using namespace std;
 
// Return the sum of first n term of AGP
int sumofNterm(int a, int d, int b, int r, int n)
{     
    // finding the each term of AGP and adding
    // it to sum.
    int sum = 0;
    for (int i = 1; i <= n ; i++)   
        sum += ((a + (i -1) * d) * (b * pow(r, i - 1))); 
    return sum;
}
 
// Driven Program
int main()
{
    int a = 1, d = 1, b = 2, r = 2, n = 3;
    cout << sumofNterm(a, d, b, r, n) << endl;
    return 0;
}


Java
// Java Program to find the sum of first n terms.
import java.io.*;
 
class GFG {
     
    // Return the sum of first n term of AGP
    static int sumofNterm(int a, int d, int b, int r, int n)
    {     
        // finding the each term of AGP and adding
        // it to sum.
        int sum = 0;
        for (int i = 1; i <= n ; i++)   
            sum += ((a + (i -1) * d) * (b * Math.pow(r, i - 1))); 
        return sum;
    }
     
    
    // Driven Program
    public static void main(String args[])
    {
        int a = 1, d = 1, b = 2, r = 2, n = 3;
        System.out.println(sumofNterm(a, d, b, r, n));
         
    }
}
 
// This code is contributed by Nikita Tiwari.


Python3
# Python3 code to find the
# sum of first n terms.
import math
 
# Return the sum of first
# n term of AGP
def sumofNterm( a , d , b ,
                    r , n ):
    # finding the each term
    # of AGP and adding it to sum.
    sum = 0
    for i in range(1,n+1):
        sum += ((a + (i -1) * d) *
            (b * math.pow(r, i - 1)))
    return int(sum)
 
# Driven Code
a = 1
d = 1
b = 2
r = 2
n = 3
print(sumofNterm(a, d, b, r, n))
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# Program to find the sum of first n terms.
using System;
 
class GFG {
     
    // Return the sum of first n term of AGP
    static int sumofNterm(int a, int d, int b, int r, int n)
    {
        // Finding the each term of AGP
        // and adding it to sum.
        int sum = 0;
        for (int i = 1; i <= n ; i++)
            sum += (int)((a + (i -1) * d) *
                         (b * Math.Pow(r, i - 1)));
        return sum;
    }
     
     
    // Driver Code
    public static void Main()
    {
        int a = 1, d = 1, b = 2, r = 2, n = 3;
        Console.Write(sumofNterm(a, d, b, r, n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


CPP
// CPPP rogram to find the sum of first n terms.
#include
using namespace std;
 
// Return the sum of first n term of AGP
int sumofNterm(int a, int d, int b, int r, int n)
{
    int ans = 0;   
    ans += a;   
    ans += ((d * r * (1 - pow(r, n-1)))/(1-r));   
    ans -= (a + (n-1)*d)*pow(r, n);   
    return (ans*b)/(1-r);
}
 
// Driven Program
int main()
{
    int a = 1, d = 1, b = 2, r = 2, n = 3;
    cout << sumofNterm(a, d, b, r, n) << endl;
    return 0;
}


Java
// Java Program to find the sum of first n terms.
 
import java.io.*;
import java.math.*;
 
class GFG {
     
    // Return the sum of first n term of AGP
    static int sumofNterm(int a, int d, int b, int r, int n)
    {
        int ans = 0;   
        ans += a;   
        ans += ((d * r * (1 - (int)(Math.pow(r, n-1))))/(1-r));   
        ans -= (a + (n-1)*d)*(int)(Math.pow(r, n));   
        return (ans*b)/(1-r);
    }
      
    
    // Driven Program
    public static void main(String args[])
    {
        int a = 1, d = 1, b = 2, r = 2, n = 3;
        System.out.println(sumofNterm(a, d, b, r, n));
         
    }
}
 
// This code is contributed by Nikita Tiwari.


Python3
# Python3 code to find
# the sum of first n terms.
import math
 
# Return the sum of
# first n term of AGP
def sumofNterm( a , d , b ,
                    r , n ):
                         
    ans = 0
    ans += a
     
    ans += ((d * r * (1 - math.pow(r, n-1))
                                )/(1-r))
     
    ans -= (a + (n-1)*d)*math.pow(r, n)
     
    return int((ans*b)/(1-r))
 
# Driven Code
a = 1
d = 1
b = 2
r = 2
n = 3
print(sumofNterm(a, d, b, r, n) )
 
# This code is contributed by "Sharad_Bhardwaj".


C#
// C# Program to find the sum of first n terms.
using System;
 
class GFG {
     
    // Return the sum of first n term of AGP
    static int sumofNterm(int a, int d, int b, int r, int n)
    {
        int ans = 0;
        ans += a;
        ans += ((d * r * (1 - (int)(Math.Pow(r, n-1))))
                                               / (1-r));
        ans -= (a + (n-1) * d) *
                (int)(Math.Pow(r, n));
         
        return (ans * b) / (1 - r);
    }
     
     
    // Driver Code
    public static void Main()
    {
        int a = 1, d = 1, b = 2, r = 2, n = 3;
        Console.Write(sumofNterm(a, d, b, r, n));
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

34

方法2 :(使用公式)

证明,

Series,
Sn = ab + (a+d)br + (a+2d)br2 + ..... + (a + (n-1)d)brn-1

Multiplying Sn by r,
rSn = abr + (a+d)br2 + (a+2d)br3 + ..... + (a + (n-1)d)brn

Subtract rSn from Sn,
(1 - r)Sn = [a + (a + d)r + (a + 2d)r2 + ...... + [a + (n-1)d]rn-1] 
           - [ar + (a + d)r2 + (a + 2d)r3 + ...... + [a + (n-1)d]rn]
          = b[a + d(r + r2 + r3 + ...... + rn-1) 
            - [a + (n-1)d]rn]
          (Using sum of geometric series Sn  = a(1 - rn-1)/(1-r))
          = b[a + dr(1 - rn-1)/(1-r) - [a + (n-1)d]rn]

以下是此方法的实现:

CPP

// CPPP rogram to find the sum of first n terms.
#include
using namespace std;
 
// Return the sum of first n term of AGP
int sumofNterm(int a, int d, int b, int r, int n)
{
    int ans = 0;   
    ans += a;   
    ans += ((d * r * (1 - pow(r, n-1)))/(1-r));   
    ans -= (a + (n-1)*d)*pow(r, n);   
    return (ans*b)/(1-r);
}
 
// Driven Program
int main()
{
    int a = 1, d = 1, b = 2, r = 2, n = 3;
    cout << sumofNterm(a, d, b, r, n) << endl;
    return 0;
}

Java

// Java Program to find the sum of first n terms.
 
import java.io.*;
import java.math.*;
 
class GFG {
     
    // Return the sum of first n term of AGP
    static int sumofNterm(int a, int d, int b, int r, int n)
    {
        int ans = 0;   
        ans += a;   
        ans += ((d * r * (1 - (int)(Math.pow(r, n-1))))/(1-r));   
        ans -= (a + (n-1)*d)*(int)(Math.pow(r, n));   
        return (ans*b)/(1-r);
    }
      
    
    // Driven Program
    public static void main(String args[])
    {
        int a = 1, d = 1, b = 2, r = 2, n = 3;
        System.out.println(sumofNterm(a, d, b, r, n));
         
    }
}
 
// This code is contributed by Nikita Tiwari.

Python3

# Python3 code to find
# the sum of first n terms.
import math
 
# Return the sum of
# first n term of AGP
def sumofNterm( a , d , b ,
                    r , n ):
                         
    ans = 0
    ans += a
     
    ans += ((d * r * (1 - math.pow(r, n-1))
                                )/(1-r))
     
    ans -= (a + (n-1)*d)*math.pow(r, n)
     
    return int((ans*b)/(1-r))
 
# Driven Code
a = 1
d = 1
b = 2
r = 2
n = 3
print(sumofNterm(a, d, b, r, n) )
 
# This code is contributed by "Sharad_Bhardwaj".

C#

// C# Program to find the sum of first n terms.
using System;
 
class GFG {
     
    // Return the sum of first n term of AGP
    static int sumofNterm(int a, int d, int b, int r, int n)
    {
        int ans = 0;
        ans += a;
        ans += ((d * r * (1 - (int)(Math.Pow(r, n-1))))
                                               / (1-r));
        ans -= (a + (n-1) * d) *
                (int)(Math.Pow(r, n));
         
        return (ans * b) / (1 - r);
    }
     
     
    // Driver Code
    public static void Main()
    {
        int a = 1, d = 1, b = 2, r = 2, n = 3;
        Console.Write(sumofNterm(a, d, b, r, n));
    }
}
 
// This code is contributed by vt_m.

的PHP


Java脚本


输出:

34