📜  求系列 0.1, 0.11, 0.111, ... 的 N 项之和

📅  最后修改于: 2022-05-13 01:56:06.760000             🧑  作者: Mango

求系列 0.1, 0.11, 0.111, ... 的 N 项之和

给定一个正整数N 。找到系列的前N 项的总和-

例子:

方法:

该序列是通过使用以下模式形成的。对于任何值 N-

推导:

以下一系列步骤可用于推导公式以找到 N 项的总和 -

插图:

以下是上述方法的实现 -

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)
{
    int a = pow(10, N);
    return (double)(N * 9 * a - a + 1)
           / (81 * a);
}
 
// Driver Code
int main()
{
    int N = 6;
    cout << findSum(N);
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
// Function to return sum of
// N term of the series
static double findSum(double N)
{
    double a = Math.pow(10, N);
    return (double)(N * 9 * a - a + 1)
           / (81 * a);
}
 
// Driver Code
    public static void main (String[] args) {
          double N = 6;
        System.out.print(findSum(N));
    }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python 3 program for the above approach
 
# Function to return sum of
# N term of the series
def findSum(N):
    a = pow(10, N)
    return (N * 9 * a - a + 1) / (81 * a)
 
# Driver Code
if __name__ == "__main__":
   
    # Value of N
    N = 6   
    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
  static double findSum(int N)
  {
    int a = (int)Math.Pow(10, N);
    return (double)(N * 9 * a - a + 1)
      / (81 * a);
  }
 
  // Driver Code
  public static void Main()
  {
    int N = 6;
    Console.Write(findSum(N));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
0.654321

时间复杂度: O(1)
辅助空间: O(1)