A number is termed as triangular number if we can represent it in the form of 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, second row has two points, third row has three points and so on. The starting triangular numbers are 1, 3 (1+2), 6 (1+2+3), 10 (1+2+3+4).
How to check if a number is Triangular?
The idea is based on the fact that n’th triangular number can be written as sum of n natural numbers, that is n*(n+1)/2. The reason for this is simple, base line of triangular grid has n dots, line above base has (n-1) dots and so on.
Method 1 (Simple)
We start with 1 and check if the number is equal to 1. If it is not, we add 2 to make it 3 and recheck with the number. We repeat this procedure until the sum remains less than or equal to the number that is to be checked for being triangular.
Following is the implementations to check if a number is triangular number.
C++
#include <iostream>
using namespace std;
bool isTriangular( int num)
{
if (num < 0)
return false ;
int sum = 0;
for ( int n=1; sum<=num; n++)
{
sum = sum + n;
if (sum==num)
return true ;
}
return false ;
}
int main()
{
int n = 55;
if (isTriangular(n))
cout << "The number is a triangular number" ;
else
cout << "The number is NOT a triangular number" ;
return 0;
}
|
Java
class GFG
{
static boolean isTriangular( int num)
{
if (num < 0 )
return false ;
int sum = 0 ;
for ( int n = 1 ; sum <= num; n++)
{
sum = sum + n;
if (sum == num)
return true ;
}
return false ;
}
public static void main (String[] args)
{
int n = 55 ;
if (isTriangular(n))
System.out.print( "The number "
+ "is a triangular number" );
else
System.out.print( "The number"
+ " is NOT a triangular number" );
}
}
|
Python3
def isTriangular(num):
if (num < 0 ):
return False
sum , n = 0 , 1
while ( sum < = num):
sum = sum + n
if ( sum = = num):
return True
n + = 1
return False
n = 55
if (isTriangular(n)):
print ( "The number is a triangular number" )
else :
print ( "The number is NOT a triangular number" )
|
C#
using System;
class GFG {
static bool isTriangular( int num)
{
if (num < 0)
return false ;
int sum = 0;
for ( int n = 1; sum <= num; n++)
{
sum = sum + n;
if (sum == num)
return true ;
}
return false ;
}
public static void Main ()
{
int n = 55;
if (isTriangular(n))
Console.WriteLine( "The number "
+ "is a triangular number" );
else
Console.WriteLine( "The number"
+ " is NOT a triangular number" );
}
}
|
PHP
<?php
function isTriangular( $num )
{
if ( $num < 0)
return false;
$sum = 0;
for ( $n = 1; $sum <= $num ; $n ++)
{
$sum = $sum + $n ;
if ( $sum == $num )
return true;
}
return false;
}
$n = 55;
if (isTriangular( $n ))
echo "The number is a triangular number" ;
else
echo "The number is NOT a triangular number" ;
?>
|
Javascript
<script>
function isTriangular(num)
{
if (num < 0)
return false ;
let sum = 0;
for (let n = 1; sum <= num; n++)
{
sum = sum + n;
if (sum == num)
return true ;
}
return false ;
}
let n = 55;
if (isTriangular(n))
document.write( "The number is a triangular number" );
else
document.write( "The number is NOT a triangular number" );
</script>
|
Output:
The number is a triangular number
Time Complexity: O(N)
Auxiliary Space: O(1)
Method 2 (Using Quadratic Equation Root Formula)
We form a quadratic equation by equating the number to the formula of sum of first ‘n’ natural numbers, and if we get atleast one value of ‘n’ that is a natural number, we say that the number is a triangular number.
Let the input number be 'num'. We consider,
n*(n+1) = num
as,
n2 + n + (-2 * num) = 0
Below is the implementation of above idea.
C++
#include <bits/stdc++.h>
using namespace std;
bool isTriangular( int num)
{
if (num < 0)
return false ;
int c = (-2 * num);
int b = 1, a = 1;
int d = (b * b) - (4 * a * c);
if (d < 0)
return false ;
float root1 = ( -b + sqrt (d)) / (2 * a);
float root2 = ( -b - sqrt (d)) / (2 * a);
if (root1 > 0 && floor (root1) == root1)
return true ;
if (root2 > 0 && floor (root2) == root2)
return true ;
return false ;
}
int main()
{
int num = 55;
if (isTriangular(num))
cout << "The number is a triangular number" ;
else
cout << "The number is NOT a triangular number" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isTriangular( int num)
{
if (num < 0 )
return false ;
int c = (- 2 * num);
int b = 1 , a = 1 ;
int d = (b * b) - ( 4 * a * c);
if (d < 0 )
return false ;
float root1 = ( -b +
( float )Math.sqrt(d)) / ( 2 * a);
float root2 = ( -b -
( float )Math.sqrt(d)) / ( 2 * a);
if (root1 > 0 && Math.floor(root1)
== root1)
return true ;
if (root2 > 0 && Math.floor(root2)
== root2)
return true ;
return false ;
}
public static void main (String[] args) {
int num = 55 ;
if (isTriangular(num))
System.out.println( "The number is"
+ " a triangular number" );
else
System.out.println ( "The number "
+ "is NOT a triangular number" );
}
}
|
Python3
import math
def isTriangular(num):
if (num < 0 ):
return False
c = ( - 2 * num)
b, a = 1 , 1
d = (b * b) - ( 4 * a * c)
if (d < 0 ):
return False
root1 = ( - b + math.sqrt(d)) / ( 2 * a)
root2 = ( - b - math.sqrt(d)) / ( 2 * a)
if (root1 > 0 and math.floor(root1) = = root1):
return True
if (root2 > 0 and math.floor(root2) = = root2):
return True
return False
n = 55
if (isTriangular(n)):
print ( "The number is a triangular number" )
else :
print ( "The number is NOT a triangular number" )
|
C#
using System;
class GFG {
static bool isTriangular( int num)
{
if (num < 0)
return false ;
int c = (-2 * num);
int b = 1, a = 1;
int d = (b * b) - (4 * a * c);
if (d < 0)
return false ;
float root1 = ( -b + ( float )Math.Sqrt(d))
/ (2 * a);
float root2 = ( -b - ( float )Math.Sqrt(d))
/ (2 * a);
if (root1 > 0 && Math.Floor(root1) == root1)
return true ;
if (root2 > 0 && Math.Floor(root2) == root2)
return true ;
return false ;
}
public static void Main () {
int num = 55;
if (isTriangular(num))
Console.WriteLine( "The number is a "
+ "triangular number" );
else
Console.WriteLine ( "The number is NOT "
+ "a triangular number" );
}
}
|
PHP
<?php
function isTriangular( $num )
{
if ( $num < 0)
return false;
$c = (-2 * $num );
$b = 1; $a = 1;
$d = ( $b * $b ) - (4 * $a * $c );
if ( $d < 0)
return false;
$root1 = (- $b + (float)sqrt( $d )) / (2 * $a );
$root2 = (- $b - (float)sqrt( $d )) / (2 * $a );
if ( $root1 > 0 && floor ( $root1 ) == $root1 )
return true;
if ( $root2 > 0 && floor ( $root2 ) == $root2 )
return true;
return false;
}
$num = 55;
if (isTriangular( $num ))
echo ( "The number is" .
" a triangular number" );
else
echo ( "The number " .
"is NOT a triangular number" );
?>
|
Javascript
<script>
function isTriangular(num)
{
if (num < 0)
return false ;
var c = (-2 * num);
var b = 1, a = 1;
var d = (b * b) - (4 * a * c);
if (d < 0)
return false ;
var root1 = (-b + Math.sqrt(d)) / (2 * a);
var root2 = (-b - Math.sqrt(d)) / (2 * a);
if (root1 > 0 && Math.floor(root1) == root1)
return true ;
if (root2 > 0 && Math.floor(root2) == root2)
return true ;
return false ;
}
var num = 55;
if (isTriangular(num))
document.write("The number is " + " a triangular number ");
else
document.write(" The number " + " is NOT a triangular number");
</script>
|
Output:
The number is a triangular number
Time Complexity: O(logn)
Auxiliary Space: O(1), since no extra space has been taken.
Last Updated :
08 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...