Hi all, another little piece of java, not for challenge, just for the fun of coding and remembering such basic knowledge as factorization/prime factorization

I have

- 2 public functions: one with sum of all prime factors, second with sum of only unique prime factors.
- 2 private functions: one checks if a int number is prime other one computes recursively the sum for the same prime factor

Also, Iām considering first prime number 2.

Java program:

```
package ro.khrypt.codekata.codeacademy;
import org.apache.log4j.Logger;
public class SumOfPrimeFactorsOfGivenNumber {
private static final Logger log = Logger.getLogger(SumOfPrimeFactorsOfGivenNumber.class);
public int sumOfUniquePrimeFactors(int n) {
int sum = 0;
if (n > 1) {
if (isPrime(n)) {
return n;
}
for (int i = 2; i < n / 2 + 1; i++) {
if (isPrime(i)) {
if (n % i == 0) {
sum += i;
}
}
}
}
return sum;
}
public int sumOfPrimeFactors(int n) {
int sum = 0;
if (n > 1) {
if (isPrime(n)) {
return n;
}
for (int i = 2; i < n / 2 + 1; i++) {
if (isPrime(i)) {
sum = localPrimesSum(n, i, sum);
}
}
}
return sum;
}
private int localPrimesSum(int n, int prime, int sum) {
if (n > 1) {
if (n % prime == 0) {
sum = localPrimesSum(n / prime, prime, sum + prime);
}
}
return sum;
}
private boolean isPrime(int n) {
if (n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17 || n == 19) {
return true;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}
```

Java test class:

```
package ro.khrypt.codekata.codeacademy;
import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.Test;
public class SumOfPrimeFactorsOfGivenNumberTest {
private static final Logger log = Logger.getLogger(SumOfPrimeFactorsOfGivenNumberTest.class);
@Test
public void testSumOfPrimeFactors() {
SumOfPrimeFactorsOfGivenNumber sn = new SumOfPrimeFactorsOfGivenNumber();
Assert.assertEquals(2, sn.sumOfPrimeFactors(2));
Assert.assertEquals(3, sn.sumOfPrimeFactors(3));
Assert.assertEquals(5, sn.sumOfPrimeFactors(5));
Assert.assertEquals(7, sn.sumOfPrimeFactors(7));
Assert.assertEquals(4, sn.sumOfPrimeFactors(4));
Assert.assertEquals(5, sn.sumOfPrimeFactors(6));
Assert.assertEquals(6, sn.sumOfPrimeFactors(9));
Assert.assertEquals(11, sn.sumOfPrimeFactors(11));
Assert.assertEquals(7, sn.sumOfPrimeFactors(12));
Assert.assertEquals(13, sn.sumOfPrimeFactors(13));
Assert.assertEquals(9, sn.sumOfPrimeFactors(14));
Assert.assertEquals(8, sn.sumOfPrimeFactors(15));
Assert.assertEquals(8, sn.sumOfPrimeFactors(16));
Assert.assertEquals(17, sn.sumOfPrimeFactors(17));
Assert.assertEquals(8, sn.sumOfPrimeFactors(18));
Assert.assertEquals(19, sn.sumOfPrimeFactors(19));
Assert.assertEquals(13, sn.sumOfPrimeFactors(22));
Assert.assertEquals(10, sn.sumOfPrimeFactors(36));
Assert.assertEquals(28, sn.sumOfPrimeFactors(456));
Assert.assertEquals(140, sn.sumOfPrimeFactors(2620));
}
@Test
public void testSumOfUniquePrimeFactors() {
SumOfPrimeFactorsOfGivenNumber sn = new SumOfPrimeFactorsOfGivenNumber();
Assert.assertEquals(2, sn.sumOfUniquePrimeFactors(2));
Assert.assertEquals(3, sn.sumOfUniquePrimeFactors(3));
Assert.assertEquals(5, sn.sumOfUniquePrimeFactors(5));
Assert.assertEquals(7, sn.sumOfUniquePrimeFactors(7));
Assert.assertEquals(2, sn.sumOfUniquePrimeFactors(4));
Assert.assertEquals(5, sn.sumOfUniquePrimeFactors(6));
Assert.assertEquals(3, sn.sumOfUniquePrimeFactors(9));
Assert.assertEquals(11, sn.sumOfUniquePrimeFactors(11));
Assert.assertEquals(5, sn.sumOfUniquePrimeFactors(12));
Assert.assertEquals(13, sn.sumOfUniquePrimeFactors(13));
Assert.assertEquals(9, sn.sumOfUniquePrimeFactors(14));
Assert.assertEquals(8, sn.sumOfUniquePrimeFactors(15));
Assert.assertEquals(2, sn.sumOfUniquePrimeFactors(16));
Assert.assertEquals(17, sn.sumOfUniquePrimeFactors(17));
Assert.assertEquals(5, sn.sumOfUniquePrimeFactors(18));
Assert.assertEquals(19, sn.sumOfUniquePrimeFactors(19));
Assert.assertEquals(13, sn.sumOfUniquePrimeFactors(22));
Assert.assertEquals(5, sn.sumOfUniquePrimeFactors(36));
Assert.assertEquals(24, sn.sumOfUniquePrimeFactors(456));
Assert.assertEquals(138, sn.sumOfUniquePrimeFactors(2620));
}
}
```

In the end, shame on me, I donāt have substantial knowledge and understanding for On ā¦

Have a nice day