给定n的值,我们需要找到级数之和,其中第i个项是第i个自然数的和。
例子 :
Input : n = 5
Output : 35
Explanation :
(1) + (1+2) + (1+2+3) + (1+2+3+4) + (1+2+3+4+5) = 35
Input : n = 10
Output : 220
Explanation :
(1) + (1+2) + (1+2+3) + .... +(1+2+3+4+.....+10) = 220
天真的方法:
以下是上述系列的实现:
C++
// CPP program to find sum of given series
#include
using namespace std;
// Function to find sum of given series
int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1 ; i <= n ; i++)
for (int j = 1 ; j <= i ; j++)
sum += j;
return sum;
}
// Driver Function
int main()
{
int n = 10;
cout << sumOfSeries(n);
return 0;
}
Java
// JAVA Code For Sum of the series
import java.util.*;
class GFG {
// Function to find sum of given series
static int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1 ; i <= n ; i++)
for (int j = 1 ; j <= i ; j++)
sum += j;
return sum;
}
/* Driver program to test above function */
public static void main(String[] args)
{
int n = 10;
System.out.println(sumOfSeries(n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python
# Python3 program to find sum of given series
# Function to find sum of series
def sumOfSeries(n):
return sum([i*(i+1)/2 for i in range(1, n + 1)])
# Driver Code
if __name__ == "__main__":
n = 10
print(sumOfSeries(n))
C#
// C# Code For Sum of the series
using System;
class GFG {
// Function to find sum of given series
static int sumOfSeries(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= i; j++)
sum += j;
return sum;
}
/* Driver program to test above function */
public static void Main()
{
int n = 10;
Console.Write(sumOfSeries(n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
C++
// CPP program to find sum of given series
#include
using namespace std;
// Function to find sum of given series
int sumOfSeries(int n)
{
return (n * (n + 1) * (2 * n + 4)) / 12;
}
// Driver Function
int main()
{
int n = 10;
cout << sumOfSeries(n);
}
Java
// JAVA Code For Sum of the series
import java.util.*;
class GFG {
// Function to find sum of given series
static int sumOfSeries(int n)
{
return (n * (n + 1) *
(2 * n + 4)) / 12;
}
/* Driver program to test above function */
public static void main(String[] args)
{
int n = 10;
System.out.println(sumOfSeries(n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python
# Python program to find sum of given series
# Function to find sum of given series
def sumOfSeries(n):
return (n * (n + 1) * (2 * n + 4)) / 12;
# Driver function
if __name__ == '__main__':
n = 10
print(sumOfSeries(n))
C#
// C# Code For Sum of the series
using System;
class GFG {
// Function to find sum of given series
static int sumOfSeries(int n)
{
return (n * (n + 1) * (2 * n + 4)) / 12;
}
/* Driver program to test above function */
public static void Main()
{
int n = 10;
Console.Write(sumOfSeries(n));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
220
高效的方法:
让系列1 +(1 + 2)+(1 + 2 + 3)+(1 + 2 + 3 + 4)…(1 + 2 + 3 + .. n)的项表示为n
正=ΣN + 1 = = 系列∑ n 1 a n = ∑ n 1的n个项之和 = Σ +Σ = * + * =
下面是上述方法的实现:
C++
// CPP program to find sum of given series
#include
using namespace std;
// Function to find sum of given series
int sumOfSeries(int n)
{
return (n * (n + 1) * (2 * n + 4)) / 12;
}
// Driver Function
int main()
{
int n = 10;
cout << sumOfSeries(n);
}
Java
// JAVA Code For Sum of the series
import java.util.*;
class GFG {
// Function to find sum of given series
static int sumOfSeries(int n)
{
return (n * (n + 1) *
(2 * n + 4)) / 12;
}
/* Driver program to test above function */
public static void main(String[] args)
{
int n = 10;
System.out.println(sumOfSeries(n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python
# Python program to find sum of given series
# Function to find sum of given series
def sumOfSeries(n):
return (n * (n + 1) * (2 * n + 4)) / 12;
# Driver function
if __name__ == '__main__':
n = 10
print(sumOfSeries(n))
C#
// C# Code For Sum of the series
using System;
class GFG {
// Function to find sum of given series
static int sumOfSeries(int n)
{
return (n * (n + 1) * (2 * n + 4)) / 12;
}
/* Driver program to test above function */
public static void Main()
{
int n = 10;
Console.Write(sumOfSeries(n));
}
}
// This code is contributed by vt_m.
的PHP
Java脚本
输出 :
220