最大的素因子程序需要aaaages - Java

时间:2012-06-22 07:45:51

标签: java prime-factoring

所以这是项目Euler的问题3。对于那些不了解的人,我必须找出最大的素数因子600851475143.我有以下代码:

import java.lang.Math;
// 600851475143
public class LargestPrimeFactor {
    public static void main(String[] stuff) {
        long num = getLong("What number do you want to analyse? ");
        long[] primes = primeGenerator(num);
        long result = 0;
        for(int i = 0; i < primes.length; i++) {
            boolean modulo2 = num % primes[i] == 0;
            if(modulo2) {
                result = primes[i];
            }
        }
        System.out.println(result);
    }
    public static long[] primeGenerator(long limit) {
        int aindex = 0;
        long[] ps = new long[primeCount(limit)];
        for(long i = 2; i < limit + 1; i++) {
            if(primeCheck(i)) {
                ps[aindex] = i;
                aindex++;
            }
        }
        return ps;
    }

    public static boolean primeCheck(long num) {
        boolean r = false;
        if(num == 2 || num == 3) {
            return true;
        }
        else if(num == 1) {
            return false;
        }
        for(long i = 2; i < Math.sqrt(num); i++) {
            boolean modulo = num % i == 0;
            if(modulo) {
                r = false;
                break;
            }
            else if(Math.sqrt(num) < i + 1 && !modulo) {
                r = true;
                break;
            }
        }
        return r;
    }
    public static int primeCount(long limit) {
        int count = 0;
        if(limit == 1 || limit == 2) {
            return 0;
        }
        for(long i = 2; i <= limit; i++) {
            if(primeCheck(i)) {
                count++;
            }
        }
        return count;
    }
public static long getLong(String prompt) {
    System.out.print(prompt + " ");
    long mrlong = input.nextLong();
    input.nextLine();
    return mrlong;
}
}

但是当我用小于600851475143(比如100000000)的东西(很多)测试程序时,程序会花费时间 - 事实上,100000000到目前为止已经花了20分钟而且还在继续。我在这里显然得到了错误的方法(是的,程序工作,我尝试用较小的数字)。任何人都可以提出一种不那么详尽的方式吗?

6 个答案:

答案 0 :(得分:5)

试试这个..

public class LargestPrimeFactor{
public static int largestPrimeFactor(long number) {
    int i;
    for (i = 2; i <= number; i++) {
        if (number % i == 0) {
            number /= i;
            i--;
        }
    }
    return i;
}

/*  change according to ur requirement. 
public static long getLong(String prompt) {
    System.out.print(prompt + " ");
    long mrlong = input.nextLong();
    input.nextLine();
    return mrlong;
}
 */

public static void main(String[] args) {
    //long num = getLong("What number do you want to analyse? ");
    System.out.println(largestPrimeFactor(600851475143l));
}
}

答案 1 :(得分:1)

public static void main(String[] args) {

    long number = 600851475143L;

    long highestPrime = -1;
    for (long i = 2; i <= number; ++i) {
        if (number % i == 0) {
            highestPrime = i;
            number /= i;
            --i;
        }
    }

    System.out.println(highestPrime);
}

答案 2 :(得分:1)

公共类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));
}

}

答案 3 :(得分:0)

我在Project Euler上做了几十个挑战。有些问题可以通过暴力解决(他们建议不要这样做),但其他问题需要“开箱即用”的思考。你无法用蛮力解决这个问题。

网上有很多帮助可以引导您朝着正确的方向前进,例如: http://thetaoishere.blogspot.com.au/2008/05/largest-prime-factor-of-number.html

答案 4 :(得分:0)

一个数字可以具有的素因子的数量总是小于该数字的sqrt,因此不需要遍历数字n来找到其最大的素因子。

请参阅此代码。

    public class LargestPrimeFactor {
        public static void main(String[] args) {

            Scanner sc=new Scanner(System.in);
            long num=sc.nextLong();

            if(num>0 && num<=2)
            {
                System.out.println("largest prime is:-" + num);
                System.exit(0);
            }

            int i=((Double)Math.sqrt(num)).intValue();
            int j=3;
            int x=0;

            //used for looping through the j value which can also be a prime. for e.g in case of 100 we might get 9 as a divisor. we need to make sure divisor is also a prime number.
            int z=0;
//same function as j but for divisor
            int y=3;
            int max=2;
//divisor is divisible
            boolean flag=false;
//we found prime factors
            boolean found=false;

            while(x<=i)
            {
                y=3;
                flag=false;

                if(num % j ==0)
                {
                    if(j>max)
                    {
                        for(z=0;z<Math.sqrt(j);z++)
                        {
                            if(j!=y && j % y==0)
                            {
                                flag=true;
                            }
                            y+=2;
                        }
                        if(!flag)
                        {
                            found=true;
                            max=j;
                        }
                    }
                }
                j+=2;
                x++;
            }
            if(found){
                System.out.println("The maximum prime is :- " + max);
            }
            else
            {
                System.out.println("The maximum prime is :- " + num);   
            }
        }
    }

答案 5 :(得分:0)

更改

for(long i = 2; i <= limit; i++)

// add the one for rounding errors in the sqrt function
new_limit = sqrt(limit) + 1;
// all even numbers are not prime 
for(long i = 3; i <= new_limit; i+=2)
{
...
}

例如,分解1,000,000而不是迭代1,000,000次 事物只需要做大约500次迭代。