0

Why is my code producing an out of range error? It produces an error if I try int, long, float, or double when I initate the variable n in the 3rd line. This is happening for all my programs in Eclipse, although 600851475143 is clearly within the range of long or float.

public class LargestPrimeFactor {
    public static void main(String arg[]){
        long n = 600851475143;
        for(float i=2; i<= n; i++){
            if((n%i==0)&&(n!=i)){
                n/=i;
                while((n%i==0)&&(n!=i)){
                    n/=i;
                }
            }
        }
        System.out.println("The greatest prime divisor is "+n+".");
    }
}
e-sushi
  • 13,786
  • 10
  • 38
  • 57
JSGandora
  • 33
  • 4
  • I'm not sure, but I think you need to place an "L" after the constant in order for Java to make it 64-bits. As in: `long n = 600851475143L;` – Ryan Marcus Jul 28 '13 at 23:30
  • Why is i a float? You can't do exact integer arithmetic with a float. – stark Jul 29 '13 at 00:57
  • Thanks, I have fixed the variable initiation and changed the type of i. I did not know that you had to append L for long data types. – JSGandora Jul 29 '13 at 04:40

0 Answers0