系列 8/10、8/100、8/1000、8/10000 的总和。 . .直到 N 项
给定一个正整数n ,任务是找到序列的和
8/10 + 8/100 + 8/1000 + 8/10000. . . till Nth term
例子:
Input: n = 3
Output: 0.888
Input: n = 5
Output: 0.88888
方法:
给定 GP 系列的第 n项的总和可以概括为-
上述公式可以通过以下一系列步骤推导出来——
The given G.P. series
Here,
Thus, using the sum of G.P. formula for r<1
Substituting the values of a and r in the above equation
插图:
Input: n = 3
Output: 0.888
Explanation:
S_{n}=\frac{8}{9}(1-(\frac{1}{10})^{3})
= 0.888 * 0.999
= 0.888
下面是上述问题的实现——
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to calculate sum of
// given series till Nth term
double sumOfSeries(double N)
{
return (8 * ((pow(10, N) - 1) / pow(10, N))) / 9;
}
// Driver code
int main()
{
double N = 5;
cout << sumOfSeries(N);
return 0;
}
Java
// Java program to implement
// the above approach
import java.util.*;
public class GFG
{
// Function to calculate sum of
// given series till Nth term
static double sumOfSeries(double N)
{
return (8
* ((Math.pow(10, N) - 1) / Math.pow(10, N)))
/ 9;
}
// Driver code
public static void main(String args[])
{
double N = 5;
System.out.print(sumOfSeries(N));
}
}
// This code is contributed by Samim Hossain Mondal.
Python3
# Python code for the above approach
# Function to calculate sum of
# given series till Nth term
def sumOfSeries(N):
return (8 * (((10 ** N) - 1) / (10 ** N))) / 9;
# Driver code
N = 5;
print(sumOfSeries(N));
# This code is contributed by gfgking
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to calculate sum of
// given series till Nth term
static double sumOfSeries(double N)
{
return (8
* ((Math.Pow(10, N) - 1) / Math.Pow(10, N)))
/ 9;
}
// Driver code
public static void Main()
{
double N = 5;
Console.WriteLine(sumOfSeries(N));
}
}
// This code is contributed by ukasp.
Javascript
输出
0.88888
时间复杂度: O(1)
辅助空间: O(1)