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))/2 = num
as,
n2 + n + (-2 * num) = 0
Below is the implementation of above approach.
// C++ program to check if a number is a triangular number
// using quadratic equation.
#include <bits/stdc++.h>
using namespace std;
// Returns true if num is triangular
bool isTriangular(int num)
{
if (num < 0)
return false;
// Considering the equation n*(n+1)/2 = num
// The equation is : a(n^2) + bn + c = 0";
int c = (-2 * num);
int b = 1, a = 1;
int d = (b * b) - (4 * a * c);
if (d < 0)
return false;
// Find roots of equation
float root1 = ( -b + sqrt(d)) / (2 * a);
float root2 = ( -b - sqrt(d)) / (2 * a);
// checking if root1 is natural
if (root1 > 0 && floor(root1) == root1)
return true;
// checking if root2 is natural
if (root2 > 0 && floor(root2) == root2)
return true;
return false;
}
// Driver code
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 program to check if a number is a
// triangular number using quadratic equation.
import java.io.*;
class GFG {
// Returns true if num is triangular
static boolean isTriangular(int num)
{
if (num < 0)
return false;
// Considering the equation
// n*(n+1)/2 = num
// The equation is :
// a(n^2) + bn + c = 0";
int c = (-2 * num);
int b = 1, a = 1;
int d = (b * b) - (4 * a * c);
if (d < 0)
return false;
// Find roots of equation
float root1 = ( -b +
(float)Math.sqrt(d)) / (2 * a);
float root2 = ( -b -
(float)Math.sqrt(d)) / (2 * a);
// checking if root1 is natural
if (root1 > 0 && Math.floor(root1)
== root1)
return true;
// checking if root2 is natural
if (root2 > 0 && Math.floor(root2)
== root2)
return true;
return false;
}
// Driver code
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");
}
}
//This code is contributed by vt_m.
// C# program to check if a number is a triangular
// number using quadratic equation.
using System;
class GFG {
// Returns true if num is triangular
static bool isTriangular(int num)
{
if (num < 0)
return false;
// Considering the equation n*(n+1)/2 = num
// The equation is : a(n^2) + bn + c = 0";
int c = (-2 * num);
int b = 1, a = 1;
int d = (b * b) - (4 * a * c);
if (d < 0)
return false;
// Find roots of equation
float root1 = ( -b + (float)Math.Sqrt(d))
/ (2 * a);
float root2 = ( -b - (float)Math.Sqrt(d))
/ (2 * a);
// checking if root1 is natural
if (root1 > 0 && Math.Floor(root1) == root1)
return true;
// checking if root2 is natural
if (root2 > 0 && Math.Floor(root2) == root2)
return true;
return false;
}
// Driver code
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");
}
}
//This code is contributed by vt_m.
<script>
// javascript program to check if a number is a
// triangular number using quadratic equation.
// Returns true if num is triangular
function isTriangular(num)
{
if (num < 0)
return false;
// Considering the equation
// n*(n+1)/2 = num
// The equation is :
// a(n^2) + bn + c = 0";
var c = (-2 * num);
var b = 1, a = 1;
var d = (b * b) - (4 * a * c);
if (d < 0)
return false;
// Find roots of equation
var root1 = (-b + Math.sqrt(d)) / (2 * a);
var root2 = (-b - Math.sqrt(d)) / (2 * a);
// checking if root1 is natural
if (root1 > 0 && Math.floor(root1) == root1)
return true;
// checking if root2 is natural
if (root2 > 0 && Math.floor(root2) == root2)
return true;
return false;
}
// Driver code
var num = 55;
if (isTriangular(num))
document.write("The number is" + " a triangular number");
else
document.write("The number " + "is NOT a triangular number");
// This code is contributed by Rajput-Ji
</script>
<?php
// PHP program to check if a number is a
// triangular number using quadratic equation.
// Returns true if num is triangular
function isTriangular($num)
{
if ($num < 0)
return false;
// Considering the equation
// n*(n+1)/2 = num
// The equation is :
// a(n^2) + bn + c = 0";
$c = (-2 * $num);
$b = 1; $a = 1;
$d = ($b * $b) - (4 * $a * $c);
if ($d < 0)
return false;
// Find roots of equation
$root1 = (-$b + (float)sqrt($d)) / (2 * $a);
$root2 = (-$b - (float)sqrt($d)) / (2 * $a);
// checking if root1 is natural
if ($root1 > 0 && floor($root1) == $root1)
return true;
// checking if root2 is natural
if ($root2 > 0 && floor($root2) == $root2)
return true;
return false;
}
// Driver code
$num = 55;
if (isTriangular($num))
echo("The number is" .
" a triangular number");
else
echo ("The number " .
"is NOT a triangular number");
// This code is contributed
// by Code_Mech.
?>
# Python3 program to check if a number is a
# triangular number using quadratic equation.
import math
# Returns True if num is triangular
def isTriangular(num):
if (num < 0):
return False
# Considering the equation n*(n+1)/2 = num
# The equation is : a(n^2) + bn + c = 0
c = (-2 * num)
b, a = 1, 1
d = (b * b) - (4 * a * c)
if (d < 0):
return False
# Find roots of equation
root1 = ( -b + math.sqrt(d)) / (2 * a)
root2 = ( -b - math.sqrt(d)) / (2 * a)
# checking if root1 is natural
if (root1 > 0 and math.floor(root1) == root1):
return True
# checking if root2 is natural
if (root2 > 0 and math.floor(root2) == root2):
return True
return False
# Driver code
n = 55
if (isTriangular(n)):
print("The number is a triangular number")
else:
print("The number is NOT a triangular number")
# This code is contributed by Smitha Dinesh Semwal
Output
The number is a triangular number
Time Complexity: O(logn)
Auxiliary Space: O(1), since no extra space has been taken.
Triangular Numbers
A number is termed 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, 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.