logic101
logic101

Reputation: 211

type of data type to represent a number

i have a few methods to calculate the largest prime factor of any mumber but it wont compile because it says my input is out of range

public class ProjectEuler3 {


    public static void main(String[] args) {
    System.out.println  (findfactors(600851475143));
    }


    public static float findfactors(long n){
        long[]factors=new long[1000];int nooffactor=0;
        int c =0;
        for( long i=2;i<n;i++){
                if (findPrime(i)){
                factors[c++]=i;
            nooffactor++;}

        }

        return factors[nooffactor-1];


    }
    public static boolean findPrime(float n){

        for(long i=2;i<n;i++){
            if(n%i==0)
                return false;
        }
        return true;
    }

}

Upvotes: 1

Views: 121

Answers (3)

jhlu87
jhlu87

Reputation: 4019

You might also want to look into the BigInteger class as you may want to factor even larger numbers

Upvotes: 1

Martin Matysiak
Martin Matysiak

Reputation: 3396

If you enter numbers in your code, they are treated as integer values. However, 600851475143 is larger than an integer can hold. Because of this, you have to append an "L" to mark it as long int:

findfactors(600851475143L)

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

Postfix you value with L to denote a constant of type long:

findfactors(600851475143L)

Upvotes: 4

Related Questions