Check if a given number is Pronic | Efficient Approach
Last Updated :
21 Sep, 2022
A pronic number is such a number which can be represented as a product of two consecutive positive integers. By multiplying these two consecutive positive integers, there can be formed a rectangle which is represented by the product or pronic number. So it is also known as Rectangular Number.
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 if a given number is pronic or not.
Mathematical Representation:
If x is a pronic number, then x=n(n+1) ? n?N0
Where, N0={0, 1, 2, 3, 4, ....}, (A set of Natural Numbers)
Examples:
Input : 56
Output : YES
Explanation: 56 = 7 * 8 i.e 56 is a product of two consecutive integers 7 and 8.
Input : 65
Output : NO
Explanation: 65 cannot be represented as a product of any two consecutive integers.
We had previously discussed an approach to check if a number is pronic or not in this article using a loop. The time Complexity of the previous algorithm is comparatively very high and in terms of Big-O asymptotic notation, it is O(?n).
In this article, we are going to explain an efficient approach with time complexity of O(log(log n). The idea is to observe that if a number can be expressed as the product of two consecutive integers then the two integers will be close to the square of root of that number. A more proper observation will lead to the fact that a number N can be represented as product of two consecutive integers only if the product of floor(sqrt(N)) and floor(sqrt(N))+1 is equal to N.
Below is the step by step algorithm of above approach:
Step 1: Evaluate the square root value of the given number.
Step 2: Calculate the floor value of that square root.
Step 3: Calculate the product of value calculated in step-2
and its next consecutive number.
Step 4: Check the product value in step-3 with the given number.
Step 4.1: If the condition satisfies,
then the number is a pronic number.
Step 4.2: Otherwise the number is not a pronic number.
Below is the implementation of above algorithm:
C++
#include<bits/stdc++.h>
using namespace std;
bool pronic_check( int n)
{
int x = ( int )( sqrt (n));
if (x*(x+1)==n)
return true ;
else
return false ;
}
int main( void )
{
int n = 56;
pronic_check(n) == true ? cout << "YES" :
cout << "NO" ;
return 0;
}
|
Java
import java.io.*;
import java.util.*;
import java.math.*;
class GFG
{
static boolean pronic_check( int n)
{
int x = ( int )(Math.sqrt(n));
if (x * (x + 1 ) == n)
return true ;
else
return false ;
}
public static void main(String[] args)
{
int n = 56 ;
if (pronic_check(n)== true )
System.out.println( "YES" );
else
System.out.println( "NO" );
}
}
|
Python3
import math
def pronic_check(n) :
x = ( int )(math.sqrt(n))
if (x * (x + 1 ) = = n):
return True
else :
return False
n = 56
if (pronic_check(n) = = True ):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
static bool pronic_check( int n)
{
int x = ( int )(Math.Sqrt(n));
if (x * (x + 1) == n)
return true ;
else
return false ;
}
public static void Main()
{
int n = 56;
if (pronic_check(n)== true )
Console.Write( "YES" );
else
Console.Write( "NO" );
}
}
|
PHP
<?php
function pronic_check( $n )
{
$x = floor (sqrt( $n ));
if ( $x * ( $x + 1) == $n )
return true;
else
return false;
}
$n = 56;
if (pronic_check( $n ) == true)
echo "YES" ;
else
echo "NO" ;
?>
|
Javascript
<script>
function pronic_check(n)
{
var x = parseInt(Math.sqrt(n));
if (x * (x + 1) == n)
return true ;
else
return false ;
}
var n = 56;
pronic_check(n) == true ? document.write( "YES" ) :
document.write( "NO" );
</script>
|
Output:
YES
Time complexity: O(logn) because it is using inbuilt sqrt function
Auxiliary space: O(1)
Share your thoughts in the comments
Please Login to comment...