refactor: change packages (#5430)

* refactor: change package

* refactor: fix name

---------

Co-authored-by: alxkm <alx@alx.com>
This commit is contained in:
Alex Klymenko
2024-08-30 11:58:24 +02:00
committed by GitHub
parent f8ff6af893
commit b0de93b3ce
10 changed files with 13 additions and 13 deletions

View File

@ -0,0 +1,47 @@
package com.thealgorithms.maths;
/**
* Utility class for computing
* <a href="https://en.wikipedia.org/wiki/Euler%27s_totient_function">Euler's totient function</a>.
*/
public final class EulersFunction {
private EulersFunction() {
}
/**
* Validates that the input is a positive integer.
*
* @param n the input number to validate
* @throws IllegalArgumentException if {@code n} is non-positive
*/
private static void checkInput(int n) {
if (n <= 0) {
throw new IllegalArgumentException("n must be positive.");
}
}
/**
* Computes the value of Euler's totient function for a given input.
* This function has a time complexity of O(sqrt(n)).
*
* @param n the input number
* @return the value of Euler's totient function for the given input
* @throws IllegalArgumentException if {@code n} is non-positive
*/
public static int getEuler(int n) {
checkInput(n);
int result = n;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
while (n % i == 0) {
n /= i;
}
result -= result / i;
}
}
if (n > 1) {
result -= result / n;
}
return result;
}
}

View File

@ -0,0 +1,66 @@
package com.thealgorithms.maths;
import java.util.Arrays;
/**
* @brief utility class implementing <a href="https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes">Sieve of Eratosthenes</a>
*/
public final class SieveOfEratosthenes {
private SieveOfEratosthenes() {
}
private static void checkInput(int n) {
if (n <= 0) {
throw new IllegalArgumentException("n must be positive.");
}
}
private static Type[] sievePrimesTill(int n) {
checkInput(n);
Type[] isPrimeArray = new Type[n + 1];
Arrays.fill(isPrimeArray, Type.PRIME);
isPrimeArray[0] = Type.NOT_PRIME;
isPrimeArray[1] = Type.NOT_PRIME;
double cap = Math.sqrt(n);
for (int i = 2; i <= cap; i++) {
if (isPrimeArray[i] == Type.PRIME) {
for (int j = 2; i * j <= n; j++) {
isPrimeArray[i * j] = Type.NOT_PRIME;
}
}
}
return isPrimeArray;
}
private static int countPrimes(Type[] isPrimeArray) {
return (int) Arrays.stream(isPrimeArray).filter(element -> element == Type.PRIME).count();
}
private static int[] extractPrimes(Type[] isPrimeArray) {
int numberOfPrimes = countPrimes(isPrimeArray);
int[] primes = new int[numberOfPrimes];
int primeIndex = 0;
for (int curNumber = 0; curNumber < isPrimeArray.length; ++curNumber) {
if (isPrimeArray[curNumber] == Type.PRIME) {
primes[primeIndex++] = curNumber;
}
}
return primes;
}
/**
* @brief finds all of the prime numbers up to the given upper (inclusive) limit
* @param n upper (inclusive) limit
* @exception IllegalArgumentException n is non-positive
* @return the array of all primes up to the given number (inclusive)
*/
public static int[] findPrimesTill(int n) {
return extractPrimes(sievePrimesTill(n));
}
private enum Type {
PRIME,
NOT_PRIME,
}
}