Check if a given number is Pronic
Last Updated :
07 Jan, 2024
The numbers that can be arranged to form a rectangle are called Rectangular Numbers (also known as Pronic numbers). The first few Pronic numbers are:
0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462 . . . . . .
Pronic number is a number which is the product of two consecutive integers, that is, a number n is a product of x and (x+1). The task is to check and print Pronic Numbers in a range.
Examples :
Input : 6
Output : Pronic Number
Explanation: 6 = 2 * 3 i.e 6 is a product
of two consecutive integers 2 and 3.
Input :56
Output :Pronic Number
Explanation: 56 = 7 * 8 i.e 56 is a product
of two consecutive integers 7 and 8.
Input : 8
Output : Not a Pronic Number
Explanation: 8 = 2 * 4 i.e 8 is a product of
2 and 4 which are not consecutive integers.
C++
#include <iostream>
#include <math.h>
using namespace std;
bool checkPronic( int x)
{
for ( int i = 0;
i <= ( int )( sqrt (x));
i++)
if (x == i * (i + 1))
return true ;
return false ;
}
int main( void )
{
for ( int i = 0; i <= 200; i++)
if (checkPronic(i))
cout << i << " " ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.math.*;
class GFG
{
static boolean checkPronic( int x)
{
for ( int i = 0 ;
i <= ( int )(Math.sqrt(x));
i++)
if (x == i * (i + 1 ))
return true ;
return false ;
}
public static void main(String[] args)
{
for ( int i = 0 ; i <= 200 ; i++)
if (checkPronic(i))
System.out.print(i + " " );
}
}
|
Python3
import math
def checkPronic (x) :
i = 0
while ( i < = ( int )(math.sqrt(x)) ) :
if ( x = = i * (i + 1 )) :
return True
i = i + 1
return False
i = 0
while (i < = 200 ) :
if checkPronic(i) :
print (i,end = " " )
i = i + 1
|
C#
using System;
class GFG
{
static bool checkPronic( int x)
{
for ( int i = 0;
i <= ( int )(Math.Sqrt(x));
i++)
if (x == i * (i + 1))
return true ;
return false ;
}
public static void Main()
{
for ( int i = 0; i <= 200; i++)
if (checkPronic(i))
Console.Write(i + " " );
}
}
|
PHP
<?php
function checkPronic( $x )
{
for ( $i = 0;
$i <= (sqrt( $x ));
$i ++)
if ( $x == $i * ( $i + 1))
return true;
return false;
}
for ( $i = 0; $i <= 200; $i ++)
if (checkPronic( $i ))
echo $i , " " ;
?>
|
Javascript
<script>
function checkPronic(x)
{
for ( var i = 0;
i <= parseInt(Math.sqrt(x));
i++)
if (x == i * (i + 1))
return true ;
return false ;
}
for ( var i = 0; i <= 200; i++)
if (checkPronic(i))
document.write(i + " " );
</script>
|
Output :
0 2 6 12 20 30 42 56 72 90 110 132 156 182
Time complexity: O( n sqrt n) to check for n numbers
Auxiliary Space : O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...