1 3 7 13 21
Plan to find the Nth term of the series 3, vii, 13, 21, 31…..
Given a number Due north, the task is to notice the Nth term of this serial:
3, seven, thirteen, 21, 31, …….
Examples:
Input: N = iv Output: 21 Explanation: Nth term = (pow(N, 2) + N + i) = (pow(4, 2) + 4 + 1) = 21 Input: Northward = 11 Output: 133
Approach:
Subtracting these two equations we get
Therefore, the Nth Term of the given series is:
Below is the implementation of the to a higher place approach:
C++
#include <iostream>
#include <math.h>
using
namespace
std;
long
long
int
getNthTerm(
long
long
int
North)
{
return
(
pow
(N, 2) + N + 1);
}
int
main()
{
long
long
int
N = 11;
cout << getNthTerm(North);
return
0;
}
Java
import
java.util.*;
class
solution
{
static
long
getNthTerm(
long
N)
{
return
((
int
)Math.pow(N,
two
) + N +
1
);
}
public
static
void
primary(String arr[])
{
long
North =
11
;
System.out.println(getNthTerm(N));
}
}
Python3
def
getNthTerm(N):
return
(
pw
(N,
2
)
+
N
+
i
)
if
__name__
=
=
'__main__'
:
Due north
=
eleven
print
(getNthTerm(N))
C#
using
System;
class
GFG
{
static
long
getNthTerm(
long
N)
{
return
((
int
)Math.Pow(N, ii) + N + 1);
}
static
public
void
Main ()
{
long
N = 11;
Console.Write(getNthTerm(North));
}
}
PHP
<?php
function
getNthTerm(
$N
)
{
render
(pow(
$Northward
, 2) +
$Due north
+ 1);
}
$Due north
= eleven;
echo
getNthTerm(
$North
);
?>
Javascript
<script>
function
getNthTerm(North)
{
return
(Math.pow(North, two) + Due north + 1);
}
let N = 11;
document.write(getNthTerm(N));
</script>
Time Complexity: O(1)
Space Complexity: O(1) since using constant variables
1 3 7 13 21,
Source: https://www.geeksforgeeks.org/program-to-find-the-nth-term-of-the-series-3-7-13-21-31/
Posted by: shawpuble1956.blogspot.com
0 Response to "1 3 7 13 21"
Post a Comment