Given series 1 2 2 3 3 3 4 4 4 4 …., find n-th term of the series. The “pattern” is obvious. There is one “1”, two “2”s three “3”s, etc.
Examples:
Input : n = 5
Output : 3
Input : n = 7
Output : 4
A naive approach is to run two loops one from 1 to n and the other from 1 to i, and for every iteration of the inner loop keep a count, whenever the count reaches n, we can break out of both the loops and i will be our answer.
#include <bits/stdc++.h> using namespace std;
// Function to find the nth term of the series int term( int n) {
int count = 0;
int result = 0;
// Outer loop from 1 to n
for ( int i = 1; i <= n; ++i) {
// Inner loop from 1 to the current value of i
for ( int j = 1; j <= i; ++j) {
// Increment the count for each iteration of the inner loop
++count;
// If the count reaches n, update the result and break out of both loops
if (count == n) {
result = i;
break ;
}
}
// If the count reaches n, break out of the outer loop
if (count == n) {
break ;
}
}
// Return the result
return result;
} int main() {
// Example usage:
int n = 5;
cout<<term(n);
return 0;
} |
public class SeriesTerm {
// Function to find the nth term of the series
static int term( int n) {
int count = 0 ;
int result = 0 ;
// Outer loop from 1 to n
for ( int i = 1 ; i <= n; ++i) {
// Inner loop from 1 to the current value of i
for ( int j = 1 ; j <= i; ++j) {
// Increment the count for each iteration of the inner loop
++count;
// If the count reaches n, update the result and break out of both loops
if (count == n) {
result = i;
break ;
}
}
// If the count reaches n, break out of the outer loop
if (count == n) {
break ;
}
}
// Return the result
return result;
}
public static void main(String[] args) {
int n = 5 ;
System.out.println(term(n));
}
} |
# Function to find the nth term of the series def term(n):
count = 0
result = 0
# Outer loop from 1 to n
for i in range ( 1 , n + 1 ):
# Inner loop from 1 to the current value of i
for j in range ( 1 , i + 1 ):
# Increment the count for each iteration of the inner loop
count + = 1
# If the count reaches n, update the result and break out of both loops
if count = = n:
result = i
break
# If the count reaches n, break out of the outer loop
if count = = n:
break
# Return the result
return result
# Example usage: n = 5
print (term(n))
# This code is contributed by Yash Agarwal(yashagarwal2852002) |
using System;
class Program
{ // Function to find the nth term of the series
static int Term( int n)
{
int count = 0;
int result = 0;
// Outer loop from 1 to n
for ( int i = 1; i <= n; ++i)
{
// Inner loop from 1 to the current value of i
for ( int j = 1; j <= i; ++j)
{
// Increment the count for each iteration of the inner loop
++count;
// If the count reaches n, update the result and break out of both loops
if (count == n)
{
result = i;
break ;
}
}
// If the count reaches n, break out of the outer loop
if (count == n)
{
break ;
}
}
// Return the result
return result;
}
static void Main()
{
int n = 5;
Console.WriteLine(Term(n));
}
} |
// Function to find the nth term of the series function term(n) {
let count = 0;
let result = 0;
// Outer loop from 1 to n
for (let i = 1; i <= n; ++i) {
// Inner loop from 1 to the current value of i
for (let j = 1; j <= i; ++j) {
// Increment the count for each iteration of the inner loop
++count;
// If the count reaches n, update the result and break out of both loops
if (count === n) {
result = i;
break ;
}
}
// If the count reaches n, break out of the outer loop
if (count === n) {
break ;
}
}
// Return the result
return result;
} // Example usage: let n = 5; console.log(term(n)); |
3
Time Complexity: O(n), as we will be using a loop to traverse n times.
Auxiliary Space: O(1), as we will not be using any extra space.
Efficient Approach:
An efficient approach will be to note down a small observation which is:
The trick is to find a pattern.
Consider numbering the given sequence as follows:
1 is at position 1
2 is at positions 2, 3
3 is at positions 4, 5, 6
4 is at positions 7, 8, 9, 10
and so on…
Notice that the last positions of the individual values form a sequence.
1, 3, 6, 10, 15, 21…
If we want a formula for the “nth” term, start by looking at it the other way around. In which term does the number “n” first appear? Taking the first term to be the “0th” term. “1” appears in term 0, “2” appears in term 1, “3” appears in term 1+2=3, “4” appears in term 1+2+3= 6, etc. The number “x” first appears in term 1 + 2 + …+ (x- 2)+ (x-1) = x(x-1)/2.
So solving for the n-th term we get n = x*(x-1)/2
Solving it using quadratic equation we get
x = ( ( 1 + sqrt(1+8*n) )/2 )
n in every case is NOT an integer which means the n-th number is not the first of a sequence of the same integer, but it is clear that the n-th integer is the integer value.
// CPP program to find the nth term of the series // 1 2 2 3 3 3 ... #include <bits/stdc++.h> using namespace std;
// function to solve the quadratic equation int term( int n)
{ // calculating the Nth term
int x = (((1) + ( double ) sqrt (1 + (8 * n))) / 2);
return x;
} // driver code to check the above function int main()
{ int n = 5;
cout << term(n);
return 0;
} |
// Java program to find the nth // term of the series 1 2 2 3 3 3 ... import java.io.*;
class Series {
// function to solve the quadratic
// equation
static int term( int n)
{
// calculating the Nth term
int x = ((( 1 ) + ( int )Math.sqrt( 1 +
( 8 * n))) / 2 );
return x;
}
// driver code to check the above function
public static void main (String[] args) {
int n = 5 ;
System.out.println(term(n));
}
} // This code is contributed by Chinmoy Lenka |
# Python program to find the nth term # of the series 1 2 2 3 3 3 ... import math
# function to solve the quadratic equation def term( n ):
# calculating the Nth term
x = ((( 1 ) + math.sqrt( 1 + ( 8 * n))) / 2 )
return x
# Driver code n = 5
print ( int (term(n)))
# This code is contributed by Sharad_Bhardwaj. |
// C# program to find the nth // term of the series 1 2 2 3 3 3 ... using System;
class Series
{ // function to solve the quadratic
// equation
static int term( int n)
{
// calculating the Nth term
int x = (((1) + ( int )Math.Sqrt(1 + (8 * n))) / 2);
return x;
}
// driver code to check the above function
public static void Main()
{
int n = 5;
Console.WriteLine(term(n));
}
} // This code is contributed by vt_m. |
<script> // Javascript program to find the nth // term of the series 1 2 2 3 3 3 ... // Function to solve the quadratic equation function term(n)
{ // Calculating the Nth term
let x = parseInt(((1) + Math.sqrt(1 + (8 * n))) / 2);
return x;
} // Driver code let n = 5; document.write(term(n)); // This code is contributed by rishavmahato348 </script> |
<?php // PHP program to find the // nth term of the series // 1 2 2 3 3 3 ... // function to solve the // quadratic equation function term( $n )
{ // calculating the Nth term
$x = (((1) + (double)sqrt(1 +
(8 * $n ))) / 2);
return $x ;
} // Driver Code $n = 5;
echo ((int)term( $n ));
// This code is contributed by Ajit. ?> |
3
Time Complexity: O(log(n)) as sqrt function takes O(log n).
Auxiliary Space: O(1), as we are not using any extra space.
Another Efficient Approach:
Another efficient approach is to find another pattern related to first and last position index of every number.
Pattern:
Consider numbering the given sequence as follows:
1 is at position 1
2 is at positions 2, 3
3 is at positions 4, 5, 6
4 is at positions 7, 8, 9, 10
5 is at positions 11, 12, 13, 14, 15
6 is at positions 16, 17, 18, 19, 20, 21
7 is at positions 22, 23, 24, 25, 26, 27, 28
and so on…
From the above notice that the first or last positions of the individual values are either their sqrt(2*first or last position) or 1+sqrt(2*first or last position), means
X = sqrt(2n) or 1+sqrt(2n)
For the above pattern let’s do the implementation:
// CPP program to find the nth term of the series // 1 2 2 3 3 3 ... #include <bits/stdc++.h> using namespace std;
// function to solve the quadratic equation int term( int n)
{ // Calculating approx. value
int x = sqrt (2 * n);
// Condition to check if the approx. number true or not
if (((x * x) - x + 2) <= 2 * n
&& ((x * x) + x) >= 2 * n)
return x;
else
return x + 1;
} // Driver's code int main()
{ int n = 5;
cout << term(n);
return 0;
} // This code is contributed by Susobhan Akhuli |
import java.util.Scanner;
public class Main {
// Function to solve the quadratic equation
static int findNthTerm( int n)
{
// Calculating the approximate value
int x = ( int )Math.sqrt( 2 * n);
// Condition to check if the approximate number is
// true or not
if (((x * x) - x + 2 ) <= 2 * n
&& ((x * x) + x) >= 2 * n)
return x;
else
return x + 1 ;
}
// Driver's code
public static void main(String[] args)
{
// Test case
int n = 5 ;
// Displaying the nth term of the series
System.out.println(findNthTerm(n));
}
} |
import math
def term(n):
x = int (math.sqrt( 2 * n))
if ((x * x) - x + 2 ) < = 2 * n and ((x * x) + x) > = 2 * n:
return x
else :
return x + 1
# Driver's code n = 5
print (term(n))
|
using System;
class Program {
// Function to solve the quadratic equation
static int Term( int n)
{
// Calculating approximate value
int x = ( int )Math.Sqrt(2 * n);
// Condition to check if the approximate number is
// true or not
if (((x * x) - x + 2) <= 2 * n
&& ((x * x) + x) >= 2 * n)
return x;
else
return x + 1;
}
// Driver's code
static void Main()
{
int n = 5;
Console.WriteLine(Term(n));
}
} |
// Function to solve the quadratic equation function term(n) {
// Calculating approx. value
const x = Math.sqrt(2 * n);
// Condition to check if the approx. number is true or not
if (((x * x) - x + 2) <= 2 * n && ((x * x) + x) >= 2 * n) {
return Math.floor(x);
} else {
return Math.floor(x) + 1;
}
} // Driver's code const n = 5; console.log(term(n)); |
3
Time Complexity: O(log(n)) as sqrt function takes O(log n).
Auxiliary Space: O(1)