📜  程序打印系列1、3、4、8、15、27、50…到N项

📅  最后修改于: 2021-05-04 21:33:59             🧑  作者: Mango

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

1, 3, 4, 8, 15, 27, 50…

例子:

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

因此,该想法是跟踪该系列的最后三个术语并找到该系列的连续术语。

下面是上述方法的实现:

C++
// C++ implementation to print the
// N terms of the series whose three
// terms are given
 
#include "bits/stdc++.h"
using namespace std;
 
// Function to print the series
void printSeries(int n, int a,
                 int b, int c)
{
 
    int d;
 
    // Generate the ith term and
    // print it
    if (n == 1) {
        cout << a << " ";
        return;
    }
    if (n == 2) {
        cout << a << " " << b << " ";
        return;
    }
 
    cout << a << " " << b
         << " " << c << " ";
 
    for (int i = 4; i <= n; i++) {
        d = a + b + c;
        cout << d << " ";
        a = b;
        b = c;
        c = d;
    }
}
 
// Driver Code
int main()
{
    int N = 7, a = 1, b = 3;
    int c = 4;
 
    // Function Call
    printSeries(N, a, b, c);
    return 0;
}


Java
// Java implementation to print the
// N terms of the series whose three
// terms are given
  
//include "bits/stdJava.h"
import java.util.*;
class GFG{
  
// Function to print the series
static void printSeries(int n, int a,
                         int b, int c)
{
    int d;
  
    // Generate the ith term and
    // print it
    if (n == 1)
    {
        System.out.print(a + " ");
        return;
    }
    if (n == 2)
    {
        System.out.print(a + " " +  b + " ");
        return;
    }
  
    System.out.print(a + " " +
                     b + " " + 
                     c + " ");
  
    for (int i = 4; i <= n; i++)
    {
        d = a + b + c;
        System.out.print(d + " ");
        a = b;
        b = c;
        c = d;
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int N = 7, a = 1, b = 3;
    int c = 4;
  
    // Function Call
    printSeries(N, a, b, c);
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation to print the
# N terms of the series whose three
# terms are given
 
# Function to print the series
def printSeries(n, a, b, c):
 
    # Generate the ith term and
    # print it
    if (n == 1):
        print(a, end = " ");
        return;
     
    if (n == 2):
        print(a, b, end = " ");
        return;
     
    print(a, b, c, end = " ");
 
    for i in range (4, n + 1):
        d = a + b + c;
        print(d, end = " ");
        a = b;
        b = c;
        c = d;
     
# Driver Code
N = 7; a = 1; b = 3;
c = 4;
 
# Function Call
printSeries(N, a, b, c);
 
# This code is contributed by Code_Mech


C#
// C# implementation to print the
// N terms of the series whose three
// terms are given
using System;
class GFG{
 
// Function to print the series
static void printSeries(int n, int a,
                        int b, int c)
{
    int d;
 
    // Generate the ith term and
    // print it
    if (n == 1)
    {
        Console.Write(a + " ");
        return;
    }
    if (n == 2)
    {
        Console.Write(a + " " +
                      b + " ");
        return;
    }
 
    Console.Write(a + " " +
                  b + " " +
                  c + " ");
 
    for(int i = 4; i <= n; i++)
    {
       d = a + b + c;
       Console.Write(d + " ");
 
       a = b;
       b = c;
       c = d;
    }
}
 
// Driver Code
public static void Main()
{
    int N = 7, a = 1, b = 3;
    int c = 4;
 
    // Function call
    printSeries(N, a, b, c);
}
}
 
// This code is contributed by rock cool


Javascript


输出:
1 3 4 8 15 27 50