algorithm: Square free integer (#3760)
* feat: Add square free integer implementation Closes #3402 * test: Add unit tests for square free integer Closes #3402 * fix: Fix failing build Changed static import for assertEquals() Closes #3402
This commit is contained in:
parent
58cf08f2fd
commit
b75dce17c3
42
src/main/java/com/thealgorithms/maths/SquareFreeInteger.java
Normal file
42
src/main/java/com/thealgorithms/maths/SquareFreeInteger.java
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
package com.thealgorithms.maths;
|
||||||
|
/*
|
||||||
|
* Java program for Square free integer
|
||||||
|
* This class has a function which checks
|
||||||
|
* if an integer has repeated prime factors
|
||||||
|
* and will return false if the number has repeated prime factors.
|
||||||
|
* true otherwise
|
||||||
|
* Wikipedia: https://en.wikipedia.org/wiki/Square-free_integer
|
||||||
|
*
|
||||||
|
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
|
||||||
|
*
|
||||||
|
* */
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class SquareFreeInteger {
|
||||||
|
/**
|
||||||
|
* This method returns whether an integer is square free
|
||||||
|
*
|
||||||
|
* @param number Integer value which is to be checked
|
||||||
|
* @return false when number has repeated prime factors
|
||||||
|
* true when number has non repeated prime factors
|
||||||
|
* @throws IllegalArgumentException when number is negative or zero
|
||||||
|
*/
|
||||||
|
public static boolean isSquareFreeInteger(int number) {
|
||||||
|
|
||||||
|
if(number <= 0) {
|
||||||
|
//throw exception when number is less than or is zero
|
||||||
|
throw new IllegalArgumentException("Number must be greater than zero.");
|
||||||
|
}
|
||||||
|
|
||||||
|
//Store prime factors of number which is passed as argument
|
||||||
|
//in a list
|
||||||
|
List<Integer> primeFactorsList = PrimeFactorization.pfactors(number);
|
||||||
|
|
||||||
|
//Create set from list of prime factors of integer number
|
||||||
|
//if size of list and set is equal then the argument passed to this method is square free
|
||||||
|
//if size of list and set is not equal then the argument passed to this method is not square free
|
||||||
|
return primeFactorsList.size() == new HashSet<>(primeFactorsList).size();
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user