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( lon开发者_Go百科g 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;
}
}
Postfix you value with L
to denote a constant of type long
:
findfactors(600851475143L)
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)
You might also want to look into the BigInteger class as you may want to factor even larger numbers
精彩评论