Largest prime factor program takes aaaages - Java

后端 未结 6 1506
一向
一向 2020-12-18 12:28

So this is problem 3 from project Euler. For those who don\'t know, I have to find out the largest prime factor of 600851475143. I have the below code:

impor         


        
6条回答
  •  醉话见心
    2020-12-18 13:03

    public class LargestPrimeFactor {

    public static boolean isPrime(long num){
        int count = 0;
        for(long i = 1; i<=num/2 ; i++){
            if(num % i==0){
                count++;
            }
        }
        if(count==1){
            return true;
        }
        return false;
    }
    
    public static String largestPrimeFactor(long num){
        String factor = "none";
        for(long i = 2; i<= num/2 ; i++){
            if(num % i==0 && isPrime(i)){
               factor = Long.toString(i); 
            }
        }
        return factor;     
    }
    public static void main(String[] args) {
        System.out.println(largestPrimeFactor(13195));
    }
    

    }

提交回复
热议问题