Find sum of the series ?3 + ?12 +……… upto N terms
Last Updated :
20 Jun, 2022
Given a number n, the task is to find the sum of first n terms of this series. Below is the series:
Examples:
Input: n = 10
Output: 95.2628
Input: n = 5
Output: 25.9808
This series can be seen as-
Below is the required implementation:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
double findSum(ll n)
{
return sqrt (3) * (n * (n + 1) / 2);
}
int main()
{
ll n = 10;
cout << findSum(n) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static double findSum( long n)
{
return Math.sqrt( 3 ) * (n *
(n + 1 ) / 2 );
}
public static void main (String[] args)
{
long n = 10 ;
System.out.println( findSum(n));
}
}
|
Python3
import math
def findSum(n):
return math.sqrt( 3 ) * (n * (n + 1 ) / 2 )
if __name__ = = '__main__' :
n = 10
print (findSum(n))
|
C#
using System;
class GFG
{
static double findSum( long n)
{
return Math.Sqrt(3) * (n *
(n + 1) / 2);
}
public static void Main ()
{
long n = 10;
Console.WriteLine( findSum(n));
}
}
|
PHP
<?php
function findSum( $n )
{
return sqrt(3) * ( $n * ( $n + 1) / 2);
}
$n = 10;
echo findSum( $n );
?>
|
Javascript
<script>
function findSum( n)
{
return Math.sqrt(3) * (n * (n + 1) / 2);
}
let n = 10;
document.write(findSum(n).toFixed(4));
</script>
|
Time complexity: O(1) since performing constant operations
Auxiliary Space: O(1) for constant space for variables
Share your thoughts in the comments
Please Login to comment...