Add another method to check Pronic number (#5919)

This commit is contained in:
Taranjeet Singh Kalsi
2024-10-26 16:49:16 +05:30
committed by GitHub
parent 1577ec4e62
commit 871e4df0d9
2 changed files with 27 additions and 24 deletions

View File

@ -21,6 +21,9 @@ public final class PronicNumber {
* @return true if input number is a pronic number, false otherwise
*/
static boolean isPronic(int inputNumber) {
if (inputNumber == 0) {
return true;
}
// Iterating from 0 to input_number
for (int i = 0; i <= inputNumber; i++) {
// Checking if product of i and (i+1) is equals input_number
@ -34,4 +37,15 @@ public final class PronicNumber {
// equals input_number
return false;
}
/**
* This method checks if the given number is pronic number or non-pronic number using square root of number for finding divisors
*
* @param number Integer value which is to be checked if is a pronic number or not
* @return true if input number is a pronic number, false otherwise
*/
public static boolean isPronicNumber(int number) {
int squareRoot = (int) Math.sqrt(number); // finding just smaller divisor of the number than its square root.
return squareRoot * (squareRoot + 1) == number;
}
}