Triangular Numbers Up To 100
A number is termed as triangular number if we tin can represent information technology in the form of triangular grid of points such that the points grade an equilateral triangle and each row contains equally many points equally the row number, i.e., the first row has 1 signal, 2nd row has two points, third row has 3 points and then on. The starting triangular numbers are one, 3 (1+2), 6 (1+2+three), x (1+2+3+4).
How to check if a number is Triangular?
The idea is based on the fact that due north'th triangular number can be written every bit sum of due north natural numbers, that is n*(n+1)/2. The reason for this is simple, base line of triangular filigree has n dots, line above base has (n-1) dots so on.
Method 1 (Simple)
We start with one and check if the number is equal to 1. If it is not, we add two to make it 3 and recheck with the number. We repeat this process 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
imitation
;
int
sum = 0;
for
(
int
northward=1; sum<=num; n++)
{
sum = sum + due north;
if
(sum==num)
return
truthful
;
}
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
course
GFG
{
static
boolean
isTriangular(
int
num)
{
if
(num <
0
)
return
false
;
int
sum =
0
;
for
(
int
north =
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))
Organization.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
):
render
False
sum
, n
=
0
,
1
while
(
sum
<
=
num):
sum
=
sum
+
due north
if
(
sum
=
=
num):
render
Truthful
north
+
=
1
render
Imitation
due north
=
55
if
(isTriangular(north)):
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
north = i; sum <= num; n++)
{
sum = sum + northward;
if
(sum == num)
return
true
;
}
return
imitation
;
}
public
static
void
Main ()
{
int
north = 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
= one;
$sum
<=
$num
;
$n
++)
{
$sum
=
$sum
+
$n
;
if
(
$sum
==
$num
)
render
truthful;
}
return
false;
}
$n
= 55;
if
(isTriangular(
$n
))
echo
"The number is a triangular number"
;
else
repeat
"The number is Non a triangular number"
;
?>
Javascript
<script>
function
isTriangular(num)
{
if
(num < 0)
return
faux
;
let sum = 0;
for
(let n = 1; sum <= num; n++)
{
sum = sum + north;
if
(sum == num)
return
true
;
}
return
simulated
;
}
allow 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
Fourth dimension 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 i value of 'n' that is a natural number, we say that the number is a triangular number.
Permit the input number be 'num'. We consider, n*(n+ane) = num as, ntwo + n + (-two * num) = 0
Beneath is the implementation of higher up idea.
C++
#include <bits/stdc++.h>
using
namespace
std;
bool
isTriangular(
int
num)
{
if
(num < 0)
return
fake
;
int
c = (-ii * num);
int
b = 1, a = 1;
int
d = (b * b) - (four * 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 &&
flooring
(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;
}
Coffee
import
java.io.*;
class
GFG {
static
boolean
isTriangular(
int
num)
{
if
(num <
0
)
return
fake
;
int
c = (-
two
* num);
int
b =
1
, a =
i
;
int
d = (b * b) - (
four
* a * c);
if
(d <
0
)
render
fake
;
float
root1 = ( -b +
(
bladder
)Math.sqrt(d)) / (
2
* a);
float
root2 = ( -b -
(
bladder
)Math.sqrt(d)) / (
2
* a);
if
(root1 >
0
&& Math.floor(root1)
== root1)
render
true
;
if
(root2 >
0
&& Math.floor(root2)
== root2)
return
true
;
render
false
;
}
public
static
void
main (Cord[] args) {
int
num =
55
;
if
(isTriangular(num))
Organization.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
):
render
Fake
c
=
(
-
2
*
num)
b, a
=
1
,
ane
d
=
(b
*
b)
-
(
4
*
a
*
c)
if
(d <
0
):
return
False
root1
=
(
-
b
+
math.sqrt(d))
/
(
two
*
a)
root2
=
(
-
b
-
math.sqrt(d))
/
(
2
*
a)
if
(root1 >
0
and
math.floor(root1)
=
=
root1):
return
Truthful
if
(root2 >
0
and
math.flooring(root2)
=
=
root2):
return
Truthful
return
False
n
=
55
if
(isTriangular(due north)):
print
(
"The number is a triangular number"
)
else
:
print
(
"The number is NOT a triangular number"
)
C#
using
System;
grade
GFG {
static
bool
isTriangular(
int
num)
{
if
(num < 0)
return
false
;
int
c = (-2 * num);
int
b = 1, a = one;
int
d = (b * b) - (4 * a * c);
if
(d < 0)
return
faux
;
float
root1 = ( -b + (
float
)Math.Sqrt(d))
/ (two * a);
float
root2 = ( -b - (
float
)Math.Sqrt(d))
/ (2 * a);
if
(root1 > 0 && Math.Floor(root1) == root1)
render
true
;
if
(root2 > 0 && Math.Flooring(root2) == root2)
render
truthful
;
return
imitation
;
}
public
static
void
Main () {
int
num = 55;
if
(isTriangular(num))
Console.WriteLine(
"The number is a "
+
"triangular number"
);
else
Panel.WriteLine (
"The number is Non "
+
"a triangular number"
);
}
}
PHP
<?php
office
isTriangular(
$num
)
{
if
(
$num
< 0)
return
false;
$c
= (-2 *
$num
);
$b
= 1;
$a
= 1;
$d
= (
$b
*
$b
) - (4 *
$a
*
$c
);
if
(
$d
< 0)
return
faux;
$root1
= (-
$b
+ (float)sqrt(
$d
)) / (2 *
$a
);
$root2
= (-
$b
- (float)sqrt(
$d
)) / (two *
$a
);
if
(
$root1
> 0 &&
flooring
(
$root1
) ==
$root1
)
render
truthful;
if
(
$root2
> 0 &&
floor
(
$root2
) ==
$root2
)
return
true;
return
faux;
}
$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 = (-two * num);
var
b = one, a = 1;
var
d = (b * b) - (four * a * c);
if
(d < 0)
return
imitation
;
var
root1 = (-b + Math.sqrt(d)) / (ii * a);
var
root2 = (-b - Math.sqrt(d)) / (2 * a);
if
(root1 > 0 && Math.floor(root1) == root1)
render
true
;
if
(root2 > 0 && Math.floor(root2) == root2)
render
truthful
;
render
false
;
}
var
num = 55;
if
(isTriangular(num))
certificate.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
Fourth dimension Complexity: O(logn)
Auxiliary Space: O(ane), since no extra space has been taken.
Please write comments if you discover anything incorrect, or yous want to share more information about the topic discussed in a higher place
Triangular Numbers Up To 100,
Source: https://www.geeksforgeeks.org/triangular-numbers/
Posted by: graydowits.blogspot.com
0 Response to "Triangular Numbers Up To 100"
Post a Comment