📌  相关文章
📜  找到系列3、13、42、108、235的n个项…

📅  最后修改于: 2021-05-07 00:57:50             🧑  作者: Mango

给定数字n,在系列3、13、42、108、235中找到第n个项。
例子:

Input : 3
Output : 42

Input : 4
Output : 108

限制条件:
1 <= T <= 100
1 <= N <= 100
天真的方法:
该系列基本上表示自然数立方和项数乘以2的总和。第一个项是单个数的总和。第二项是两个数字的总和,依此类推。
例子:

n = 2
2nd term equals to sum of 1st term and 8 i.e
A2 = A1 + 23 + n*2
   = 1 + 8 + 4
   = 13

Similarly,
A3 = A2 + 33 + n*2
   = 9 + 27 + 6
   = 42 and so on..

一个简单的解决方案是将前n个自然数立方体和项数相乘2。

C++
// C++ program to find n-th term of
// series 3, 13, 42, 108, 235…
#include 
using namespace std;
 
// Function to generate a fixed number
int magicOfSequence(int N)
{
    int sum = 0;
    for (int i = 1; i <= N; i++)
        sum += (i*i*i + i*2);
    return sum;   
}
 
// Driver Method
int main()
{
    int N = 4;
    cout << magicOfSequence(N) << endl;
    return 0;
}


Java
// Java Program to Finding n-th term
// of series 3, 13, 42, 108, 235 ...
 
class GFG {
 
// Function to generate
// a fixed number
public static int magicOfSequence(int N)
{
    int sum = 0;
    for (int i = 1; i <= N; i++)
        sum += (i * i * i + i * 2);
    return sum;
}
 
// Driver Method
public static void main(String args[])
{
    int N = 4;
    System.out.println(magicOfSequence(N));
}
}
 
// This code is contributed by Jaideep Pyne


Python3
# Python3 program to
# find n-th term of
# series 3, 13, 42, 108, 235…
 
# Function to generate
# a fixed number
def magicOfSequence(N) :
 
    sum = 0
    for i in range(1, N + 1) :
        sum += (i * i * i + i * 2)
    return sum;
 
# Driver Code
N = 4
print(magicOfSequence(N))
 
# This code is contributed by vij.


C#
// C# Program to Finding
// n-th term of series
// 3, 13, 42, 108, 235 ...
using System;
 
class GFG
{
     
// Function to generate
// a fixed number
public static int magicOfSequence(int N)
{
    int sum = 0;
    for (int i = 1; i <= N; i++)
        sum += (i * i * i + i * 2);
    return sum;
}
 
// Driver Code
static public void Main ()
{
    int N = 4;
    Console.WriteLine(magicOfSequence(N));
}
}
 
// This code is contributed
// by ajit


PHP


Javascript


C++
// A formula based C++ program to find sum
// of series with cubes of first n natural
// numbers
#include 
using namespace std;
 
int magicOfSequence(int N)
{
    return (N * (N + 1) / 2) + 2 * N;
}
 
// Driver Function
int main()
{
    int N = 6;
    cout << magicOfSequence(N);
    return 0;
}


Java
// A formula based Java program to find sum
// of series with cubes of first n natural
// numbers
class GFG {
     
    static int magicOfSequence(int N)
    {
        return (N * (N + 1) / 2) + 2 * N;
    }
     
    // Driver Function
    public static void main(String[] args)
    {
        int N = 6;
        System.out.println(magicOfSequence(N));
    }
}
 
// This code is contributed by Smitha.


Python 3
# A formula based Python program to find sum
# of series with cubes of first n natural
# numbers
def magicOfSequence(N):
 
    return (N * (N + 1) / 2) + 2 * N
 
# Driver Function
N = 6
print(int(magicOfSequence(N)))
 
# This code is contributed by Smitha.


C#
// A formula based C# program to find sum
// of series with cubes of first n natural
// numbers
using System;
 
class GFG {
     
    static int magicOfSequence(int N)
    {
        return (N * (N + 1) / 2) + 2 * N;
    }
     
    // Driver Function
    public static void Main()
    {
        int N = 6;
        Console.Write(magicOfSequence(N));
    }
}
 
// This code is contributed by Smitha.


PHP


Javascript


输出:
120

该解决方案的时间复杂度为O(n)。
高效的方法:
我们知道前n个自然数的立方和是(n *(n + 1)/ 2) 2 。我们还知道,如果我们将第i个项乘以2并将所有项相加,我们将得到n个项之和为2 * n。
因此,我们的结果是(n *(n + 1)/ 2) 2 + 2 * n。
例子 :

C++

// A formula based C++ program to find sum
// of series with cubes of first n natural
// numbers
#include 
using namespace std;
 
int magicOfSequence(int N)
{
    return (N * (N + 1) / 2) + 2 * N;
}
 
// Driver Function
int main()
{
    int N = 6;
    cout << magicOfSequence(N);
    return 0;
}

Java

// A formula based Java program to find sum
// of series with cubes of first n natural
// numbers
class GFG {
     
    static int magicOfSequence(int N)
    {
        return (N * (N + 1) / 2) + 2 * N;
    }
     
    // Driver Function
    public static void main(String[] args)
    {
        int N = 6;
        System.out.println(magicOfSequence(N));
    }
}
 
// This code is contributed by Smitha.

的Python 3

# A formula based Python program to find sum
# of series with cubes of first n natural
# numbers
def magicOfSequence(N):
 
    return (N * (N + 1) / 2) + 2 * N
 
# Driver Function
N = 6
print(int(magicOfSequence(N)))
 
# This code is contributed by Smitha.

C#

// A formula based C# program to find sum
// of series with cubes of first n natural
// numbers
using System;
 
class GFG {
     
    static int magicOfSequence(int N)
    {
        return (N * (N + 1) / 2) + 2 * N;
    }
     
    // Driver Function
    public static void Main()
    {
        int N = 6;
        Console.Write(magicOfSequence(N));
    }
}
 
// This code is contributed by Smitha.

的PHP


Java脚本


输出:
33

时间复杂度: O(1)