# 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∈N_{0}Where, N_{0}={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` `?>` |

## Javascript

`<script>` `// Javascript program to check if a number is pronic or not` `// function to check Pronic Number` `function` `pronic_check(n)` `{` ` ` `var` `x = parseInt(Math.sqrt(n));` ` ` `// Checking Pronic Number by` ` ` `// multiplying consecutive numbers` ` ` `if` `(x * (x + 1) == n)` ` ` `return` `true` `;` ` ` `else` ` ` `return` `false` `;` `}` `// Driver Code` `var` `n = 56; ` `pronic_check(n) == ` `true` `? document.write(` `"YES"` `) :` `document.write(` `"NO"` `);` `// This code is contributed by noob2000.` `</script>` |

Output:

YES

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**