I recently started learning Java, and I am now trying to solve some Eulerproject problems.
The task is: What is the largest prime factor of the number 600851475143 ?
I was able to create this code, but I get an error:
Code
package exercises;
import java.util.ArrayList;
public class Euler3 {
// Main code
public static void main(String[] args) {
System.out.println( getPrimeFactors(15) );
}
// Code for breaking a number to prime factors
public static ArrayList getPrimeFactors(int n){
ArrayList factors = new ArrayList();
int d = 2;
while(n > 1 ){
while(n%d == 0 ){
factors.add(d);
n /= d;
}
d +=d;
}
return factors;
}
}
Error:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at exercises.Euler3.getPrimeFactors(Euler3.java:22)
at exercises.Euler3.main(Euler3.java:9)
What am I doing wrong?
Thanks