Check if a given number is Pronic | Efficient Approach
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 Naturral 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
// C/C++ program to check if a number is pronic or not #include<bits/stdc++.h> using namespace std; // function to check Pronic Number bool pronic_check(int n) { int x = (int)(sqrt(n)); // Checking Pronic Number by // multiplying consecutive numbers if (x*(x+1)==n) return true; else return false; } // Driver Code int main(void) { int n = 56; pronic_check(n) == true? cout << "YES" : cout << "NO"; return 0; } |
Java
// Java program to check if a number is pronic or not import java.io.*; import java.util.*; import java.math.*; class GFG { // Function to check Pronic Number static boolean pronic_check(int n) { int x = (int)(Math.sqrt(n)); // Checking Pronic Number by // multiplying consecutive numbers if (x * (x + 1) == n) return true; else return false; } // Driver Code public static void main(String[] args) { int n = 56; if (pronic_check(n)==true) System.out.println("YES"); else System.out.println("NO"); } } |
Python3
# Python program to check if a number is pronic or not import math # function to check Pronic Number def pronic_check(n) : x = (int)(math.sqrt(n)) # Checking Pronic Number by multiplying # consecutive numbers if (x*(x + 1)== n): return True else: return False # Driver Code n = 56 if (pronic_check(n)==True): print("YES") else: print("NO") |
C#
// C# program to check if a number is // pronic or not using System; class GFG { // Function to check Pronic Number static bool pronic_check(int n) { int x = (int)(Math.Sqrt(n)); // Checking Pronic Number by // multiplying consecutive numbers if (x * (x + 1) == n) return true; else return false; } // Driver Code public static void Main() { int n = 56; if (pronic_check(n)==true) Console.Write("YES"); else Console.Write("NO"); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to check if a // number is pronic or not // function to check Pronic Number function pronic_check($n) { $x = floor(sqrt($n)); // Checking Pronic Number by // multiplying consecutive numbers if ($x * ($x + 1) == $n) return true; else return false; } // Driver Code $n = 56; if (pronic_check($n) == true) echo "YES" ; else echo "NO"; // This code is contributed by Sam007 ?> |
Output:
YES
Recommended Posts:
- Check if a given number is Pronic
- An efficient way to check whether n-th Fibonacci number is multiple of 10
- Write an Efficient Method to Check if a Number is Multiple of 3
- Efficient program to print the number of factors of n numbers
- Space efficient iterative method to Fibonacci number
- Write an Efficient C Program to Reverse Bits of a Number
- Efficient program to print all prime factors of a given number
- Rectangular (or Pronic) Numbers
- Efficient way to multiply with 7
- Check if a number is divisible by all prime divisors of another number
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Efficient program to calculate e^x
- Program to check whether a number is Proth number or not
- Check if a number with even number of digits is palindrome or not
- Efficient Program to Compute Sum of Series 1/1! + 1/2! + 1/3! + 1/4! + .. + 1/n!
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Sam007



