Find n-th term of series 2, 10, 30, 68, 130 …
Last Updated :
17 Feb, 2023
Given a series 2, 10, 30, 68, 130 … Identify the pattern in the series and find the nth value in the series. Indices are starting from 1 . 1 <= n <= 200
Examples:
Input : n = 12
Output : 1740
Input : n = 200
Output : 8000200
If observed carefully, the pattern of the series can be noticed as n^3 + n.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findValueAtX( int n)
{
return (n * n * n) + n;
}
int main()
{
cout << findValueAtX(10) << endl;
cout << findValueAtX(2) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findValueAtX( int n)
{
return (n * n * n) + n;
}
public static void main(String[] args)
{
System.out.println(findValueAtX( 10 ));
System.out.println(findValueAtX( 2 ));
}
}
|
Python3
def findValueAtX(n):
return (n * n * n) + n
print (findValueAtX( 10 ))
print (findValueAtX( 2 ))
|
C#
using System;
class GFG {
static int findValueAtX( int n)
{
return (n * n * n) + n;
}
public static void Main()
{
Console.WriteLine(findValueAtX(10));
Console.WriteLine(findValueAtX(2));
}
}
|
PHP
<?php
function findValueAtX( $n )
{
return ( $n * $n * $n ) + $n ;
}
echo findValueAtX(10), "\n" ;
echo findValueAtX(2);
?>
|
Javascript
<script>
function findValueAtX(n)
{
return (n * n * n) + n;
}
document.write(findValueAtX(10) + "</br>" );
document.write(findValueAtX(2));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...