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> |
0.428571
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!