Find the sum of N terms of the series 1/1*3, 1/3*5, 1/5*7, ….
Given a positive integer, N. Find the sum of the first N term of the series-
1/1*3, 1/3*5, 1/5*7, ….
Examples:
Input: N = 3
Output: 0.428571
Input: N = 1
Output: 0.333333
Approach: The sequence is formed by using the following pattern. For any value N-
SN = N / (2 * N + 1)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
double findSum( int N) {
return ( double )N / (2 * N + 1);
}
int main()
{
int N = 3;
cout << findSum(N);
}
|
Java
import java.util.*;
class GFG
{
public static double findSum( int N)
{
return ( double )N / ( 2 * N + 1 );
}
public static void main(String[] args)
{
int N = 3 ;
System.out.print(findSum(N));
}
}
|
Python3
def findSum(N):
return N / ( 2 * N + 1 )
if __name__ = = "__main__" :
N = 3
print (findSum(N))
|
C#
using System;
class GFG
{
public static double findSum( int N)
{
return ( double )N / (2 * N + 1);
}
public static void Main()
{
int N = 3;
Console.Write(findSum(N));
}
}
|
Javascript
<script>
function findSum(N) {
return N / (2 * N + 1);
}
let N = 3;
document.write(findSum(N));
</script>
|
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
20 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...