Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/main/java/com/thealgorithms/maths/PronicNumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ private 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
Expand All @@ -34,4 +37,15 @@ static boolean isPronic(int inputNumber) {
// 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;
}
}
37 changes: 13 additions & 24 deletions src/test/java/com/thealgorithms/maths/PronicNumberTest.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
package com.thealgorithms.maths;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class PronicNumberTest {

@Test
void testForPronicNumber() {
// given
int number = 30;

// when
boolean result = PronicNumber.isPronic(number);

// then
assertTrue(result);
@ParameterizedTest
@ValueSource(ints = {0, 2, 6, 12, 20, 30, 42, 110, 272, 380, 420, 1260, 2550})
void testForPronicNumber(final int number) {
Assertions.assertTrue(PronicNumber.isPronic(number));
Assertions.assertTrue(PronicNumber.isPronicNumber(number));
}

@Test
void testForNonPronicNumber() {
// given
int number = 21;

// when
boolean result = PronicNumber.isPronic(number);

// then
assertFalse(result);
@ParameterizedTest
@ValueSource(ints = {1, 4, 21, 36, 150, 2500})
void testForNonPronicNumber(final int number) {
Assertions.assertFalse(PronicNumber.isPronic(number));
Assertions.assertFalse(PronicNumber.isPronicNumber(number));
}
}