Project Euler #3 takes forever in Java

后端 未结 9 788
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 03:01

Problem #3 on Project Euler is:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 60085147514

相关标签:
9条回答
  • 2020-12-04 03:24

    It's not the perfect solution, but it will work for 600851475143.

    public static void main(String[] args) {
        long number= 600851475143L;
        int rootOfNumber = (int)Math.sqrt(number)+10;
        for(int i = rootOfNumber; i > 2; i--) {
            if(number % i == 0) {
                if(psudoprime(i)) {
                    System.out.println(i);
                    break;
                }
            }
        }
    
    }
    
    public static boolean psudoprime(int num) {
        for(int i = 2; i < 100; i++) {
            if(num % i == 0) {
                return false;
            }
        }
        return true;
    }
    
    0 讨论(0)
  • 2020-12-04 03:25

    Here's pseudocode for integer factorization by trial division:

    define factors(n)
    
        z = 2
    
        while (z * z <= n)
    
            if (n % z == 0)
                output z
                n /= z
    
            else
                z++
    
        output n
    

    The easiest way to understand this is by an example. Consider the factorization of n = 13195. Initially z = 2, but dividing 13195 by 2 leaves a remainder of 1, so the else clause sets z = 3 and we loop. Now n is not divisible by 3, or by 4, but when z = 5 the remainder when dividing 13195 by 5 is zero, so output 5 and divide 13195 by 5 so n = 2639 and z = 5 is unchanged. Now the new n = 2639 is not divisible by 5 or 6, but is divisible by 7, so output 7 and set n = 2639 / 7 = 377. Now we continue with z = 7, and that leaves a remainder, as does division by 8, and 9, and 10, and 11, and 12, but 377 / 13 = 29 with no remainder, so output 13 and set n = 29. At this point z = 13, and z * z = 169, which is larger than 29, so 29 is prime and is the final factor of 13195, so output 29. The complete factorization is 5 * 7 * 13 * 29 = 13195.

    There are better algorithms for factoring integers using trial division, and even more powerful algorithms for factoring integers that use techniques other than trial division, but the algorithm shown above will get you started, and is sufficient for Project Euler #3. When you're ready for more, look here.

    0 讨论(0)
  • 2020-12-04 03:27

    Although not in Java, I think you can probably make out the following. Basically, cutting down on the iterations by only testing odd divisors and up to the square root of a number is needed. Here is a brute force approach that gives an instant result in C#.

    static bool OddIsPrime (long oddvalue)  // test an odd >= 3 
    {
        // Only test odd divisors.
        for (long i = 3; i <= Math.Sqrt(oddvalue); i += 2)
        {
            if (value % i == 0)
                return false;
        }
        return true;
    }
    
    static void Main(string[] args)
    {
        long max = 600851475143;   // an odd value
        long maxFactor = 0;
    
        // Only test odd divisors of MAX. Limit search to Square Root of MAX.
        for (long i = 3; i <= Math.Sqrt(max); i += 2)
        {
            if (max % i == 0)
            {
                if (OddIsPrime(i))  // i is odd
                {
                    maxFactor = i;
                }
            }
        }
        Console.WriteLine(maxFactor.ToString());
        Console.ReadLine();
    }
    
    0 讨论(0)
提交回复
热议问题