求系列 1+22+333+4444+……到 n 项的和
原文:https://www . geesforgeks . org/find-sum-the-series-1223334444-upto-n-terms/
给定一个数字 N,任务是找到下面系列的和直到第 N 项:
1+22+333+4444+…最多 n 项
示例 :
Input: N = 3
Output: 356
Input: N = 10
Output: 12208504795
进场:
以下是上述方法的实现:
C++
// CPP program to find the sum
// of given series
#include <iostream>
#include <math.h>
using namespace std;
// Function to calculate sum
int findSum(int n)
{
// Return sum
return (pow(10, n + 1) * (9 * n - 1) + 10) /
pow(9, 3) - n * (n + 1) / 18;
}
// Driver code
int main()
{
int n = 3;
cout << findSum(n);
return 0;
}
Java 语言(一种计算机语言,尤用于创建网站)
// Java Program to find
// Sum of first n terms
import java.util.*;
class solution
{
static int calculateSum(int n)
{
// Returning the final sum
return ((int)Math.pow(10, n + 1) * (9 * n - 1) + 10) /
(int)Math.pow(9, 3) - n * (n + 1) / 18;
}
// Driver code
public static void main(String ar[])
{
// no. of terms to find the sum
int n=3;
System.out.println("Sum= "+ calculateSum(n));
}
}
//This code is contributed by Surendra_Gangwar
Python 3
# Python program to find the sum of given series.
# Function to calculate sum
def solve_sum(n):
# Return sum
return (pow(10, n + 1)*(9 * n - 1)+10)/pow(9, 3)-n*(n + 1)/18
# driver code
n = 3
print(int(solve_sum(n)))
C
// C# Program to find
// Sum of first n terms
using System;
class solution
{
static int calculateSum(int n)
{
// Returning the final sum
return ((int)Math.Pow(10, n + 1) * (9 * n - 1) + 10) /
(int)Math.Pow(9, 3) - n * (n + 1) / 18;
}
// Driver code
public static void Main()
{
// no. of terms to find the sum
int n=3;
Console.WriteLine("Sum= "+ calculateSum(n));
}
}
//This code is contributed by inder_verma.
服务器端编程语言(Professional Hypertext Preprocessor 的缩写)
<?php
// PHP program to find the sum
// of given series
// Function to calculate sum
function findSum($n)
{
// Return sum
return (pow(10, $n + 1) *
(9 * $n - 1) + 10) /
pow(9, 3) - $n * ($n + 1) / 18;
}
// Driver code
$n = 3;
echo findSum($n);
// This code is contributed
// by inder_verma.
?>
java 描述语言
<script>
// Javascript Program to find
// Sum of first n terms
function calculateSum( n)
{
// Returning the const sum
return (parseInt(Math.pow(10, n + 1)) * (9 * n - 1) + 10) /
parseInt(Math.pow(9, 3)) - n * (n + 1) / 18;
}
// Driver code
// no. of terms to find the sum
let n = 3;
document.write("Sum= " + calculateSum(n));
// This code is contributed by 29AjayKumar
</script>
Output:
356
版权属于:月萌API www.moonapi.com,转载请注明出处