Add Pronic Number (#2867)
* Add Pronic Number * Update directory * Add unit tests for Pronic Number implementation Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:
parent
98656cb0cd
commit
32cdf02afb
15
DIRECTORY.md
15
DIRECTORY.md
@ -225,6 +225,7 @@
|
||||
* [GenericRoot](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/GenericRoot.java)
|
||||
* [HarshadNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/HarshadNumber.java)
|
||||
* [JugglerSequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/JugglerSequence.java)
|
||||
* [KaprekarNumbers](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/KaprekarNumbers.java)
|
||||
* [KeithNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/KeithNumber.java)
|
||||
* [KrishnamurthyNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/KrishnamurthyNumber.java)
|
||||
* [LeonardoNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/LeonardoNumber.java)
|
||||
@ -241,6 +242,7 @@
|
||||
* [NumberOfDigits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/NumberOfDigits.java)
|
||||
* [PalindromeNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PalindromeNumber.java)
|
||||
* [ParseInteger](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/ParseInteger.java)
|
||||
* [PascalTriangle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PascalTriangle.java)
|
||||
* [PerfectCube](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PerfectCube.java)
|
||||
* [PerfectNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PerfectNumber.java)
|
||||
* [PerfectSquare](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PerfectSquare.java)
|
||||
@ -250,6 +252,7 @@
|
||||
* [PowRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PowRecursion.java)
|
||||
* [PrimeCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PrimeCheck.java)
|
||||
* [PrimeFactorization](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PrimeFactorization.java)
|
||||
* [PronicNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PronicNumber.java)
|
||||
* [PythagoreanTriple](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/PythagoreanTriple.java)
|
||||
* [ReverseNumber](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/ReverseNumber.java)
|
||||
* [RomanNumeralUtil](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/RomanNumeralUtil.java)
|
||||
@ -278,6 +281,7 @@
|
||||
* [TwoSumProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/misc/TwoSumProblem.java)
|
||||
* [WordBoggle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/misc/WordBoggle.java)
|
||||
* others
|
||||
* [ArrayLeftRotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/ArrayLeftRotation.java)
|
||||
* [BankersAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/BankersAlgorithm.java)
|
||||
* [BestFit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/BestFit.java)
|
||||
* [BFPRT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/others/BFPRT.java)
|
||||
@ -344,6 +348,7 @@
|
||||
* [LowerBound](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/LowerBound.java)
|
||||
* [MonteCarloTreeSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/MonteCarloTreeSearch.java)
|
||||
* [PerfectBinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/PerfectBinarySearch.java)
|
||||
* [QuickSelect](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/QuickSelect.java)
|
||||
* [SaddlebackSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java)
|
||||
* [SquareRootBinarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/SquareRootBinarySearch.java)
|
||||
* [TernarySearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/searches/TernarySearch.java)
|
||||
@ -383,6 +388,7 @@
|
||||
* [TreeSort](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/sorts/TreeSort.java)
|
||||
* strings
|
||||
* [Alphabetical](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Alphabetical.java)
|
||||
* [Anagrams](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Anagrams.java)
|
||||
* [CharactersSame](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/CharactersSame.java)
|
||||
* [CheckAnagrams](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/CheckAnagrams.java)
|
||||
* [CheckVowels](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/CheckVowels.java)
|
||||
@ -397,3 +403,12 @@
|
||||
* [Rotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Rotation.java)
|
||||
* [Upper](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Upper.java)
|
||||
* [WordLadder](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/WordLadder.java)
|
||||
* test
|
||||
* maths
|
||||
* [KaprekarNumbersTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/KaprekarNumbersTest.java)
|
||||
* [PascalTriangleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PascalTriangleTest.java)
|
||||
* [PronicNumberTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/PronicNumberTest.java)
|
||||
* others
|
||||
* [ArrayLeftRotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java)
|
||||
* searches
|
||||
* [QuickSelectTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/searches/QuickSelectTest.java)
|
||||
|
38
src/main/java/com/thealgorithms/maths/PronicNumber.java
Normal file
38
src/main/java/com/thealgorithms/maths/PronicNumber.java
Normal file
@ -0,0 +1,38 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
/*
|
||||
* Java program for Pronic Number
|
||||
* Pronic Number: A number n is a pronic number if
|
||||
* it is equal to product of two consecutive numbers m and m+1.
|
||||
* Wikipedia: https://en.wikipedia.org/wiki/Pronic_number
|
||||
*
|
||||
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
|
||||
*
|
||||
* */
|
||||
|
||||
public class PronicNumber {
|
||||
|
||||
/**
|
||||
* This method checks if the given number is pronic number or non-pronic number
|
||||
*
|
||||
* @param input_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
|
||||
*/
|
||||
static boolean isPronic(int input_number) {
|
||||
|
||||
//Iterating from 0 to input_number
|
||||
for(int i = 0; i <= input_number; i++) {
|
||||
|
||||
//Checking if product of i and (i+1) is equals input_number
|
||||
if(i * (i+1) == input_number && i != input_number) {
|
||||
|
||||
//return true if product of i and (i+1) is equals input_number
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//return false if product of i and (i+1) for all values from 0 to input_number is not equals input_number
|
||||
return false;
|
||||
}
|
||||
}
|
34
src/test/java/com/thealgorithms/maths/PronicNumberTest.java
Normal file
34
src/test/java/com/thealgorithms/maths/PronicNumberTest.java
Normal file
@ -0,0 +1,34 @@
|
||||
package com.thealgorithms.maths;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class PronicNumberTest {
|
||||
|
||||
@Test
|
||||
void testForPronicNumber() {
|
||||
|
||||
//given
|
||||
int number = 30;
|
||||
|
||||
//when
|
||||
boolean result = PronicNumber.isPronic(number);
|
||||
|
||||
//then
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testForNonPronicNumber() {
|
||||
|
||||
//given
|
||||
int number = 21;
|
||||
|
||||
//when
|
||||
boolean result = PronicNumber.isPronic(number);
|
||||
|
||||
//then
|
||||
assertFalse(result);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user