📌  相关文章
📜  求出?3 +?12 +………的序列之和,最多N个项

📅  最后修改于: 2021-04-24 18:43:25             🧑  作者: Mango

给定数字n,任务是找到该系列的前n个项的总和。以下是系列:

    $$\sqrt{3}+\sqrt{12}+\sqrt{27}+\sqrt{48}+\sqrt{75}+\cdots$$

例子:

Input: n = 10
Output: 95.2628

Input: n = 5
Output: 25.9808

该系列可以看作是-

    $$1\sqrt{3}+2\sqrt{3}+3\sqrt{3}+4\sqrt{3}+5\sqrt{3}+\cdots$$ Now, take $\sqrt{3} $ as common, we get- $$\sqrt{3}\left[1+2+3+4+5+\cdots\right]$$ $$sum=\sqrt{3}*\left(\frac{n*(n+1)}{2}\right)$$

以下是所需的实现:

C++
// C++ implementation of above approach
#include 
#define ll long long int
using namespace std;
 
// Function to find the sum
double findSum(ll n)
{
 
    // Apply AP formula
    return sqrt(3) * (n * (n + 1) / 2);
}
// Driver code
int main()
{
    // number of terms
    ll n = 10;
 
    cout << findSum(n) << endl;
    return 0;
}


Java
// Java implementation of
// above approach
import java.io.*;
 
class GFG
{
 
// Function to find the sum
static double findSum(long n)
{
    // Apply AP formula
    return Math.sqrt(3) * (n *
                    (n + 1) / 2);
}
// Driver code
public static void main (String[] args)
{
    // number of terms
    long n = 10;
     
    System.out.println( findSum(n));
}
}
 
// This code is contributed
// by inder_verma..


Python3
# Python3 implementation of above approach
 
#Function to find the sum
import math
def findSum(n):
    # Apply AP formula
    return math.sqrt(3) * (n * (n + 1) / 2)
 
# Driver code
# number of terms
if __name__=='__main__':
    n = 10
    print(findSum(n))
 
# This code is contributed by sahilshelangia


C#
// C# implementation of
// above approach
using System;
 
class GFG
{
 
// Function to find the sum
static double findSum(long n)
{
    // Apply AP formula
    return Math.Sqrt(3) * (n *
                    (n + 1) / 2);
}
// Driver code
public static void Main ()
{
    // number of terms
    long n = 10;
     
    Console.WriteLine( findSum(n));
}
}
 
// This code is contributed
// by inder_verma..


PHP


Javascript


输出:
95.2628