Program to find the Nth number of the series 2, 10, 24, 44, 70…..
Last Updated :
27 Aug, 2022
Given a number N, the task is to find the Nth (N may be up to 10^18) term of this series:
2, 10, 24, 44, 70…..
The answer can be very large so print answer under modulo 10^9+9.
Examples:
Input: N = 2
Output: 10
Input: N = 5
Output: 70
Approach:The formula for Nth term will be:
Nth term = 3*n*n – n
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000009
int NthTerm( long long n)
{
long long x = (3 * n * n) % mod;
return (x - n + mod) % mod;
}
int main()
{
long long N = 4;
cout << NthTerm(N);
return 0;
}
|
C
#include <stdio.h>
#define mod 1000000009
int NthTerm( long long n)
{
long long x = (3 * n * n) % mod;
return (x - n + mod) % mod;
}
int main()
{
long long N = 4;
printf ( "%d" ,NthTerm(N));
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
static long NthTerm( long n)
{
long x = ( 3 * n * n) % 1000000009 ;
return (x - n + 1000000009 ) % 1000000009 ;
}
public static void main(String args[])
{
long N = 4 ;
System.out.println(NthTerm(N));
}
}
|
Python3
def NthTerm(N) :
x = ( 3 * N * N) % 1000000009
return ((x - N + 1000000009 ) % 1000000009 )
if __name__ = = "__main__" :
N = 4
print (NthTerm(N))
|
C#
using System;
class GFG
{
static long NthTerm( long n)
{
long x = (3 * n * n) % 1000000009;
return (x - n + 1000000009) % 1000000009;
}
public static void Main()
{
long N = 4;
Console.Write(NthTerm(N));
}
}
|
PHP
<?php
function NthTerm( $n )
{
$mod = 1000000009;
$x = (3 * $n * $n ) % $mod ;
return ( $x - $n + $mod ) % $mod ;
}
$N = 4;
echo NthTerm( $N );
?>
|
Javascript
<script>
function NthTerm( n) {
let x = (3 * n * n) % 1000000009;
return (x - n + 1000000009) % 1000000009;
}
let N = 4;
document.write(NthTerm(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...