求系列 2、5、8、11、14 的前 N 项的总和。
给定一个正整数N ,任务是找到序列的前 N 项的和
2, 5, 8, 11, 14..
例子:
Input: N = 5
Output: 40
Input: N = 10
Output: 155
方法:
1st term = 2
2nd term = (2 + 3) = 5
3rd term = (5 + 3) = 8
4th term = (8 + 3) = 11
.
.
Nth term = (2 + (N – 1) * 3) = 3N – 1
该序列是通过使用以下模式形成的。对于任何值 N-
Here,
a is the first term
d is the common difference
上述解决方案可以通过以下一系列步骤得出 -
The series-
2, 5, 8, 11, …., till N terms
is in A.P. with first term of the series a = 2 and common difference d = 3
Sum of N terms of an A.P. is
插图:
Input: N = 5
Output: 40
Explanation:
a = 2
d = 3
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to return
// Nth term of the series
int nth(int n, int a1, int d)
{
return (a1 + (n - 1) * d);
}
// Function to return sum of
// Nth term of the series
int sum(int a1, int nterm, int n)
{
return n * (a1 + nterm) / 2;
}
// Driver code
int main()
{
// Value of N
int N = 5;
// First term
int a = 2;
// Common difference
int d = 3;
// finding last term
int nterm = nth(N, a, d);
cout << sum(a, nterm, N) << endl;
return 0;
}
C
// C program to implement
// the above approach
#include
// Function to return
// Nth term of the series
int nth(int n, int a1, int d)
{
return (a1 + (n - 1) * d);
}
// Function to return
// sum of Nth term of the series
int sum(int a1, int nterm, int n)
{
return n * (a1 + nterm) / 2;
}
// Driver code
int main()
{
// Value of N
int N = 5;
// First term
int a = 2;
// Common difference
int d = 3;
// Finding last term
int nterm = nth(N, a, d);
printf("%d", sum(a, nterm, N));
return 0;
}
Java
// Java program to implement
// the above approach
import java.io.*;
class GFG {
// Driver code
public static void main(String[] args)
{
// Value of N
int N = 5;
// First term
int a = 2;
// Common difference
int d = 3;
// Finding Nth term
int nterm = nth(N, a, d);
System.out.println(sum(a, nterm, N));
}
// Function to return
// Nth term of the series
public static int nth(int n,
int a1, int d)
{
return (a1 + (n - 1) * d);
}
// Function to return
// sum of Nth term of the series
public static int sum(int a1,
int nterm, int n)
{
return n * (a1 + nterm) / 2;
}
}
Python3
# Python code for the above approach
# Function to return
# Nth term of the series
def nth(n, a1, d):
return (a1 + (n - 1) * d);
# Function to return sum of
# Nth term of the series
def sum(a1, nterm, n):
return n * (a1 + nterm) / 2;
# Driver code
# Value of N
N = 5;
# First term
a = 2;
# Common difference
d = 3;
# finding last term
nterm = nth(N, a, d);
print((int)(sum(a, nterm, N)))
# This code is contributed by gfgking
C#
using System;
public class GFG
{
// Function to return
// Nth term of the series
public static int nth(int n, int a1, int d)
{
return (a1 + (n - 1) * d);
}
// Function to return
// sum of Nth term of the series
public static int sum(int a1, int nterm, int n)
{
return n * (a1 + nterm) / 2;
}
static public void Main()
{
// Code
// Value of N
int N = 5;
// First term
int a = 2;
// Common difference
int d = 3;
// Finding Nth term
int nterm = nth(N, a, d);
Console.Write(sum(a, nterm, N));
}
}
// This code is contributed by Potta Lokesh
Javascript
输出:
40