求以下序列的和:2,22,222,……到n项。 例如:
null
Input : 2Output: 1.9926Input : 3Output: 23.9112
A. 简单解决方案 就是一个接一个地计算这些项,然后把它们加到结果中。 上述问题可能是 有效地 使用以下公式求解:
C++
// CPP program to find sum of series // 2, 22, 222, .. #include <bits/stdc++.h> using namespace std; // function which return the // the sum of series float sumOfSeries( int n) { return 0.0246 * ( pow (10, n) - 1 - (9 * n)); } // driver code int main() { int n = 3; cout << sumOfSeries(n); return 0; } |
JAVA
// JAVA Code for Sum of the // sequence 2, 22, 222,... import java.util.*; class GFG { // function which return the // the sum of series static double sumOfSeries( int n) { return 0.0246 * (Math.pow( 10 , n) - 1 - ( 9 * n)); } /* Driver program */ public static void main(String[] args) { int n = 3 ; System.out.println(sumOfSeries(n)); } } // This code is contributed by Arnav Kr. Mandal. |
Python3
# Python3 code to find # sum of series # 2, 22, 222, .. import math # function which return # the sum of series def sumOfSeries( n ): return 0.0246 * (math. pow ( 10 , n) - 1 - ( 9 * n)) # driver code n = 3 print ( sumOfSeries(n)) # This code is contributed by "Sharad_Bhardwaj". |
C#
// C# Code for Sum of the // sequence 2, 22, 222,... using System; class GFG { // Function which return the // the sum of series static double sumOfSeries( int n) { return 0.0246 * (Math.Pow(10, n) - 1 - (9 * n)); } // Driver Code public static void Main() { int n = 3; Console.Write(sumOfSeries(n)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find sum // of series 2, 22, 222, .. // function which return the // the sum of series function sumOfSeries( $n ) { return 0.0246 * (pow(10, $n ) - 1 - (9 * $n )); } // Driver Code $n = 3; echo (sumOfSeries( $n )); // This code is contributed by Ajit. ?> |
Javascript
<script> // JavaScript program for Sum of the // sequence 2, 22, 222,... // function which return the // the sum of series function sumOfSeries(n) { return 0.0246 * (Math.pow(10, n) - 1 - (9 * n)); } // Driver code let n = 3; document.write(sumOfSeries(n)); </script> |
输出:
23.9112
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END