求系列 1/1*3、1/3*5、1/5*7、……的 N 项之和。
给定一个正整数N 。找到系列的前N 项的总和-
1/1*3, 1/3*5, 1/5*7, ….
例子:
Input: N = 3
Output: 0.428571
Input: N = 1
Output: 0.333333
方法:使用以下模式形成序列。对于任何值 N-
SN = N / (2 * N + 1)
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
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
输出
0.428571
时间复杂度:O(1)
辅助空间:O(1)