My goal is to find the sum of the exponents of the prime factors of an integer.
I wrote the code below with the complexities specified here (I am not 100% sure of the complexities though):
Runtime complexity:
findPrimesSmaller
: \$O(n \log(\log n))\$exponentInDecomposition
: \$O(n)\$primeExponentsCount
: \$O(n^2 \log(\log n))\$
Space Complexity:
findPrimesSmaller
: \$O(\log N)\$ (?)exponentInDecomposition
: \$O(1)\$primeExponentsCount: \$O(\log N)\$
import static org.junit.Assert.*;
import java.util.*;
import org.junit.*;
public class PrimeCount {
// Given a number n and its associated prime numbers decomposition:
// n = p1 ^ (alpha1) * p2 ^ (alpha2) * ... * pn ^ (alphan)
// where all the pi's are prime numbers smaller than n
// return the sum of the alphai's
public int primeExponentsCount(int n) {
// First generate the list of prime numbers smaller than n
ArrayList<Integer> candidatePrimeNumbers = findPrimesSmaller(n);
// initialize the sum
int result = 0;
for (int i : candidatePrimeNumbers) {
result += exponentInDecomposition(i, n);
}
return result;
}
public ArrayList<Integer> findPrimesSmaller(int n) {
int[] allNumbers = new int[n + 1];
// Initialize the list with all the numbers smaller than n
for (int i = 0; i <= n; i++) {
allNumbers[i] = i;
}
// Apply a Sieves of Eratosthenes method to keep only the prime numbers
for (int i = 2; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (j > 0 && j % i == 0) {
allNumbers[j] = -1;
}
}
}
ArrayList<Integer> primeNumbers = new ArrayList<Integer>();
for (int i = 2; i <= n; i++) { // Remove 0 and 1 from list => start counting at 2
if (allNumbers[i] > 0) {
primeNumbers.add(i);
}
}
return primeNumbers;
}
public int exponentInDecomposition(int p, int n) {
int result = 0;
while (n % Math.pow(p, result) == 0 && n / Math.pow(p, result) >= 1) {
result++;
}
result--;
return result;
}
@Test
public void test_exponentZeroDecomposition() {
int a = exponentInDecomposition(3, 5);
assertEquals(0, a);
}
@Test
public void test_exponentNonZeroDecomposition() {
int a = exponentInDecomposition(3, 9);
assertEquals(2, a);
}
@Test
public void primesSmaller10() {
ArrayList<Integer> primesLessThan10 = findPrimesSmaller(10);
int two = primesLessThan10.get(0);
int three = primesLessThan10.get(1);
int five = primesLessThan10.get(2);
int seven = primesLessThan10.get(3);
assertEquals(2, two);
assertEquals(3, three);
assertEquals(5, five);
assertEquals(7, seven);
}
@Test
public void testCount0() {
int zero = primeExponentsCount(0);
assertEquals(0, zero);
}
@Test
public void testCount1() {
int zero = primeExponentsCount(1);
assertEquals(0, zero);
}
@Test
public void testCount10() {
int two = primeExponentsCount(10);
assertEquals(2, two);
}
@Test
public void testCount13() {
int one = primeExponentsCount(13);
assertEquals(1, one);
}
public static void main(String[] args) {
PrimeCount e = new PrimeCount();
e.test_exponentZeroDecomposition();
e.test_exponentNonZeroDecomposition();
e.primesSmaller10();
e.testCount0();
e.testCount1();
e.testCount10();
e.testCount13();
}
}