📜  程序打印系列2,15,41,80,132,197…直到N个字

📅  最后修改于: 2021-04-26 06:58:35             🧑  作者: Mango

给定数字N ,任务是打印以下系列的前N个术语:

2 15 41 80 132 197 275 366 470 587…

例子:

方法:从给定的系列中,我们可以找到第N个项的公式:

所以:

然后对[1,N]范围内的数字进行迭代以使用上述公式查找所有项并打印出来。

下面是上述方法的实现:

C++
// C++ implementation to print the
// given with the given Nth term
 
#include "bits/stdc++.h"
using namespace std;
 
// Function to print the series
void printSeries(int N)
{
 
    int ith_term = 0;
 
    // Generate the ith term and
    for (int i = 1; i <= N; i++) {
 
        ith_term = (13 * i * (i - 1)) / 2 + 2;
        cout << ith_term << ", ";
    }
}
 
// Driver Code
int main()
{
    int N = 7;
 
    printSeries(N);
    return 0;
}


Java
// Java implementation to print the
// given with the given Nth term
import java.util.*;
 
class GFG{
 
// Function to print the series
static void printSeries(int N)
{
    int ith_term = 0;
     
    // Generate the ith term and
    for(int i = 1; i <= N; i++)
    {
       ith_term = (13 * i * (i - 1)) / 2 + 2;
       System.out.print(ith_term + ", ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 7;
 
    printSeries(N);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation to print the
# given with the given Nth term
 
# Function to print the series
def printSeries(N):
     
    ith_term = 0;
 
    # Generate the ith term and
    for i in range(1, N + 1):
        ith_term = (13 * i * (i - 1)) / 2 + 2;
        print(int(ith_term), ", ", end = "");
 
# Driver Code
if __name__ == '__main__':
     
    N = 7;
     
    printSeries(N);
     
# This code is contributed by amal kumar choubey


C#
// C# implementation to print the
// given with the given Nth term
using System;
 
class GFG{
 
// Function to print the series
static void printSeries(int N)
{
    int ith_term = 0;
     
    // Generate the ith term and
    for(int i = 1; i <= N; i++)
    {
       ith_term = (13 * i * (i - 1)) / 2 + 2;
       Console.Write(ith_term + ", ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 7;
 
    printSeries(N);
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
2, 15, 41, 80, 132, 197, 275,