-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathSquareFreeInteger.java
45 lines (40 loc) · 1.61 KB
/
SquareFreeInteger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.thealgorithms.maths.Prime;
/*
* 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 final class SquareFreeInteger {
private 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();
}
}