📌  相关文章
📜  找出1、2、3、6、9、18、27、54…的序列之和,直到N个项

📅  最后修改于: 2021-04-27 18:50:04             🧑  作者: Mango

给定数字N ,任务是找到以下序列的和,直到N个项。

例子:

方法:从给定的系列中,找到第N个项的公式:

1st term = 1
2nd term = 2 = 2 * 1
3rd term = 3 = 3/2 * 2
4th term = 6 = 2 * 3
5th term = 9 = 3/2 * 6
6th term = 18 = 2 * 9
.
.
Nth term = [2 * (N-1)th term], if N is even
           [3/2 * (N-1)th term], if N is odd

所以:

然后对[1,N]范围内的数字进行迭代以使用上述公式查找所有项并计算其总和。
方法:通过观察给定序列中的模式,序列的下一个数字将分别乘以23/2
下面是上述方法的实现:

C++
*** QuickLaTeX cannot compile formula:
 

*** Error message:
Error: Nothing to show, formula is empty


Java
// C++ program for the above series
#include 
using namespace std;
 
// Function to find the sum of series
void printSeriesSum(int N)
{
    double sum = 0;
 
    int a = 1;
    int cnt = 0;
 
    // Flag to find the multiplicating
    // factor.. i.e, by 2 or 3/2
    bool flag = true;
 
    // First term
    sum += a;
 
    while (cnt < N) {
 
        int nextElement;
 
        // If flag is true, multiply by 2
        if (flag) {
            nextElement = a * 2;
            sum += nextElement;
            flag = !flag;
        }
 
        // If flag is false, multiply by 3/2
        else {
            nextElement = a * 3 / 2;
            sum += nextElement;
            flag = !flag;
        }
 
        // Update the previous element
        // to nextElement
        a = nextElement;
        cnt++;
    }
 
    // Print the sum
    cout << sum << endl;
}
 
// Driver Code
int main()
{
 
    int N = 8;
 
    printSeriesSum(N);
    return 0;
}


Python3
// Java program for the above series
class GFG {
     
    // Function to find the sum of series
    static void printSeriesSum(int N)
    {
        double sum = 0;
     
        int a = 1;
        int cnt = 0;
     
        // Flag to find the multiplicating
        // factor.. i.e, by 2 or 3/2
        boolean flag = true;
     
        // First term
        sum += a;
     
        while (cnt < N) {
     
            int nextElement;
     
            // If flag is true, multiply by 2
            if (flag == true) {
                nextElement = a * 2;
                sum += nextElement;
                flag = !flag;
            }
     
            // If flag is false, multiply by 3/2
            else {
                nextElement = a * 3 / 2;
                sum += nextElement;
                flag = !flag;
            }
     
            // Update the previous element
            // to nextElement
            a = nextElement;
            cnt++;
        }
     
        // Print the sum
        System.out.println(sum);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
     
        int N = 8;
     
        printSeriesSum(N);
    }
}
// This code is contributed by AnkitRai01


C#
# Python3 program for the above series
 
# Function to find the sum of series
def printSeriesSum(N) :
 
    sum = 0;
 
    a = 1;
    cnt = 0;
 
    # Flag to find the multiplicating
    # factor.. i.e, by 2 or 3/2
    flag = True;
 
    # First term
    sum += a;
 
    while (cnt < N) :
 
        nextElement = None;
 
        # If flag is true, multiply by 2
        if (flag) :
            nextElement = a * 2;
            sum += nextElement;
            flag = not flag;
 
        # If flag is false, multiply by 3/2
        else :
            nextElement = a * (3 / 2);
            sum += nextElement;
            flag = not flag;
 
        # Update the previous element
        # to nextElement
        a = nextElement;
        cnt += 1
 
    # Print the sum
    print(sum);
 
# Driver Code
if __name__ == "__main__" :
 
 
    N = 8;
 
    printSeriesSum(N);
     
    # This code is contributed by AnkitRai01


Javascript
// C# program for the above series
using System;
 
class GFG {
     
    // Function to find the sum of series
    static void printSeriesSum(int N)
    {
        double sum = 0;
     
        int a = 1;
        int cnt = 0;
     
        // Flag to find the multiplicating
        // factor.. i.e, by 2 or 3/2
        bool flag = true;
     
        // First term
        sum += a;
     
        while (cnt < N) {
     
            int nextElement;
     
            // If flag is true, multiply by 2
            if (flag == true) {
                nextElement = a * 2;
                sum += nextElement;
                flag = !flag;
            }
     
            // If flag is false, multiply by 3/2
            else {
                nextElement = a * 3 / 2;
                sum += nextElement;
                flag = !flag;
            }
     
            // Update the previous element
            // to nextElement
            a = nextElement;
            cnt++;
        }
     
        // Print the sum
        Console.WriteLine(sum);
    }
     
    // Driver Code
    public static void Main (string[] args)
    {
     
        int N = 8;
     
        printSeriesSum(N);
    }
}
 
// This code is contributed by AnkitRai01


输出: