Friday, July 5, 2024
HomeData ModellingData Structure & AlgorithmFind the sum of N terms of the series 1/1*3, 1/3*5, 1/5*7,...

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++




// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to return sum of
// N term of the series
 
double findSum(int N) {
  return (double)N / (2 * N + 1);
}
 
// Driver Code
 
int main()
{
    int N = 3;
 
    cout << findSum(N);
}


Java




// JAVA program to implement
// the above approach
import java.util.*;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  public static double findSum(int N)
  {
    return (double)N / (2 * N + 1);
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int N = 3;
 
    System.out.print(findSum(N));
  }
}
 
// This code is contributed by Taranpreet


Python3




# Python 3 program for the above approach
 
# Function to return sum of
# N term of the series
 
def findSum(N):
  return N / (2 * N + 1)
 
 
# Driver Code
if __name__ == "__main__":
   
    # Value of N
    N = 3   
    print(findSum(N))
 
# This code is contributed by Abhishek Thakur.


C#




// C# program to implement
// the above approach
using System;
class GFG
{
 
  // Function to return sum of
  // N term of the series
  public static double findSum(int N)
  {
    return (double)N / (2 * N + 1);
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 3;
 
    Console.Write(findSum(N));
  }
}
 
// This code is contributed by gfgking


Javascript




<script>
// Javascript program to implement
// the above approach
 
// Function to return sum of
// N term of the series
 
function findSum(N) {
  return N / (2 * N + 1);
}
 
// Driver Code
 
let N = 3;
 
document.write(findSum(N));
 
// This code is contributed by Palak Gupta
</script>


Output

0.428571

 Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Commit to GfG’s Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.

Last Updated :
20 Aug, 2022
Like Article
Save Article


Previous


Next


Share your thoughts in the comments

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments