Find the sequence number of a triangular number
Last Updated :
29 Jan, 2024
Given an integer N print the sequence number of the given Triangular Number. If the number is not a triangular number then print -1.
Â
A number is termed as a triangular number if we can represent it in the form of a triangular grid of points such that the points form an equilateral triangle and each row contains as many points as the row number, i.e., the first row has one point, the second row has two points, the third row has three points and so on.Â
First 10 triangular number are: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55.Â
Â
Examples:Â
Â
Input: N = 21Â
Output:6Â
Explanation:Â
Since 15 is a 6th Triangular Number.
Input: N = 12Â
Output:-1Â
Explanation:Â
Since 12 is not a Triangular NumberÂ
Approach:Â
- Since triangular numbers are the sum of natural numbers so can be generalized as a quadratic equation.
C++
#include<bits/stdc++.h>
using namespace std;
int main()
{
int N = 21;
int A = sqrt (2 * N + 0.25) - 0.5;
int B = A;
if (B != A)
cout << "-1" ;
else
cout << B;
}
|
Java
import java.util.*;
class GFG{
public static void main(String args[])
{
int N = 21 ;
int A = ( int )(Math.sqrt( 2 * N + 0.25 ) - 0.5 );
int B = A;
if (B != A)
System.out.print( "-1" );
else
System.out.print(B);
}
}
|
Python3
import math
N = 21
A = math.sqrt( 2 * N + 0.25 ) - 0.5
B = int (A)
if B ! = A:
print ( - 1 )
else :
print (B)
|
C#
using System;
class GFG{
public static void Main()
{
int N = 21;
int A = ( int )(Math.Sqrt(2 * N + 0.25) - 0.5);
int B = A;
if (B != A)
Console.Write( "-1" );
else
Console.Write(B);
}
}
|
Javascript
<script>
let N = 21;
let A = Math.sqrt(2 * N + 0.25) - 0.5;
let B = A;
if (B != A)
document.write( "-1" );
else
document.write(B);
</script>
|
Time Complexity: O(logN)Â as sqrt function is being used
Auxiliary Space: O(1)
Approach 02 : This approach works because it iterates through the triangular numbers until it either finds the exact match or number greater than N.
C++
#include <iostream>
using namespace std;
int GFG( int N) {
int triangular = 8;
int n = 1;
while (triangular < N) {
triangular += n;
n++;
}
if (triangular == N) {
return n - 1;
} else {
return -1;
}
}
int main() {
int N;
cin >> N;
int result = GFG(N);
if (result != -1) {
cout << "Output: " << result << endl;
} else {
cout << "Output: -1" << endl;
}
return 0;
}
|
Java
import java.util.Scanner;
public class GFG {
static int findTriangularRow( int N) {
int triangular = 8 ;
int n = 1 ;
while (triangular < N) {
triangular += n;
n++;
}
if (triangular == N) {
return n - 1 ;
} else {
return - 1 ;
}
}
public static void main(String[] args) {
int N = 12 ;
int result = findTriangularRow(N);
if (result != - 1 ) {
System.out.println( "Output: " + result);
} else {
System.out.println( "Output: -1" );
}
}
}
|
Python3
def GFG(N):
triangular = 8
n = 1
while triangular < N:
triangular + = n
n + = 1
if triangular = = N:
return n - 1
else :
return - 1
def main():
N = 12
result = GFG(N)
if result ! = - 1 :
print ( "Output:" , result)
else :
print ( "Output: -1" )
if __name__ = = "__main__" :
main()
|
C#
using System;
class MainClass
{
static int GFG( int N)
{
int triangular = 8;
int n = 1;
while (triangular < N)
{
triangular += n;
n++;
}
if (triangular == N)
{
return n - 1;
}
else
{
return -1;
}
}
public static void Main( string [] args)
{
int N;
Console.Write( "Enter N: " );
N = Convert.ToInt32(Console.ReadLine());
int result = GFG(N);
if (result != -1)
{
Console.WriteLine($ "Output: {result}" );
}
else
{
Console.WriteLine( "Output: -1" );
}
}
}
|
Javascript
function findTriangularRow(N) {
let triangular = 8;
let n = 1;
while (triangular < N) {
triangular += n;
n++;
}
if (triangular === N) {
return n - 1;
} else {
return -1;
}
}
let N = parseInt(prompt( "Enter N:" ));
let result = findTriangularRow(N);
if (result !== -1) {
console.log(`Output: ${result}`);
} else {
console.log( "Output: -1" );
}
|
Time complexity: O(sqrt(N))
Auxiliary space complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...