 # rootulp's solution

## to Prime Factors in the Java Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Compute the prime factors of a given natural number.

A prime number is only evenly divisible by itself and 1.

Note that 1 is not a prime number.

## Example

What are the prime factors of 60?

• Our first divisor is 2. 2 goes into 60, leaving 30.
• 2 goes into 30, leaving 15.
• 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.
• 3 goes cleanly into 15, leaving 5.
• 3 does not go cleanly into 5. The next possible factor is 4.
• 4 does not go cleanly into 5. The next possible factor is 5.
• 5 does go cleanly into 5.
• We're left only with 1, so now, we're done.

Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.

You can check this yourself:

• 2 * 2 * 3 * 5
• = 4 * 15
• = 60
• Success!

# Java Tips

Since this exercise has difficulty 5 it doesn't come with any starter implementation. This is so that you get to practice creating classes and methods which is an important part of programming in Java. It does mean that when you first try to run the tests, they won't compile. They will give you an error similar to:

`````` path-to-exercism-dir\exercism\java\name-of-exercise\src\test\java\ExerciseClassNameTest.java:14: error: cannot find symbol
ExerciseClassName exerciseClassName = new ExerciseClassName();
^
symbol:   class ExerciseClassName
location: class ExerciseClassNameTest
``````

This error occurs because the test refers to a class that hasn't been created yet (`ExerciseClassName`). To resolve the error you need to add a file matching the class name in the error to the `src/main/java` directory. For example, for the error above you would add a file called `ExerciseClassName.java`.

When you try to run the tests again you will get slightly different errors. You might get an error similar to:

``````  constructor ExerciseClassName in class ExerciseClassName cannot be applied to given types;
ExerciseClassName exerciseClassName = new ExerciseClassName("some argument");
^
required: no arguments
found: String
reason: actual and formal argument lists differ in length
``````

This error means that you need to add a constructor to your new class. If you don't add a constructor, Java will add a default one for you. This default constructor takes no arguments. So if the tests expect your class to have a constructor which takes arguments, then you need to create this constructor yourself. In the example above you could add:

``````ExerciseClassName(String input) {

}
``````

That should make the error go away, though you might need to add some more code to your constructor to make the test pass!

You might also get an error similar to:

``````  error: cannot find symbol
assertEquals(expectedOutput, exerciseClassName.someMethod());
^
symbol:   method someMethod()
location: variable exerciseClassName of type ExerciseClassName
``````

This error means that you need to add a method called `someMethod` to your new class. In the example above you would add:

``````String someMethod() {
return "";
}
``````

Make sure the return type matches what the test is expecting. You can find out which return type it should have by looking at the type of object it's being compared to in the tests. Or you could set your method to return some random type (e.g. `void`), and run the tests again. The new error should tell you which type it's expecting.

After having resolved these errors you should be ready to start making the tests pass!

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

## Source

The Prime Factors Kata by Uncle Bob http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### PrimeFactorsCalculatorTest.java

``````import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;

import static org.junit.Assert.assertEquals;

public class PrimeFactorsCalculatorTest {

private PrimeFactorsCalculator primeFactorsCalculator;

@Before
public void setUp() {
primeFactorsCalculator = new PrimeFactorsCalculator();
}

@Test
public void testNoFactors() {
assertEquals(Collections.emptyList(), primeFactorsCalculator.calculatePrimeFactorsOf(1L));
}

@Ignore("Remove to run test")
@Test
assertEquals(Collections.singletonList(2L), primeFactorsCalculator.calculatePrimeFactorsOf(2L));
}

@Ignore("Remove to run test")
@Test
public void testSquareOfAPrime() {
assertEquals(Arrays.asList(3L, 3L), primeFactorsCalculator.calculatePrimeFactorsOf(9L));
}

@Ignore("Remove to run test")
@Test
public void testCubeOfAPrime() {
assertEquals(Arrays.asList(2L, 2L, 2L), primeFactorsCalculator.calculatePrimeFactorsOf(8L));
}

@Ignore("Remove to run test")
@Test
public void testProductOfPrimesAndNonPrimes() {
assertEquals(Arrays.asList(2L, 2L, 3L), primeFactorsCalculator.calculatePrimeFactorsOf(12L));
}

@Ignore("Remove to run test")
@Test
public void testProductOfPrimes() {
assertEquals(Arrays.asList(5L, 17L, 23L, 461L), primeFactorsCalculator.calculatePrimeFactorsOf(901255L));
}

@Ignore("Remove to run test")
@Test
public void testFactorsIncludingALargePrime() {
assertEquals(Arrays.asList(11L, 9539L, 894119L), primeFactorsCalculator.calculatePrimeFactorsOf(93819012551L));
}

}``````
``````import java.util.List;
import java.util.ArrayList;

public class PrimeFactors {

public static List<Integer> getForNumber(double input) {
List<Integer> factors = new ArrayList<Integer>();
double num = input;

for (int i = 2; i <= num; i++) {
if (num % i == 0) {
num = num / i;
i = i - 1;
}
}

return factors;
}

}``````