I am trying to solve some project Euler problems and for whatever reason the following code gives me a division by zero error whenever I try to run it with large numbers. Can anyone tell me why?
import java.util.Scanner;
public class Problem3LargestPrimeFactor {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Please enter the number to find the largest prime factor for.");
long num = input.nextLong();
int largest = 1;
boolean isPrime = true;
for(int i = 2; i < num; i++) {
if(num % i == 0) {
for(int u = 2; u < i; u++){
if(i % u == 0)
isPrime = false;
}
if(isPrime)
largest = i;
}
}
}
}
Now I'm aware that this is not the most efficient way to design the algorithm but can anyone tell me what is going on here?