qwerty
qwerty

Reputation: 830

Project Euler #3 in Java; program not outputting result

I am trying solve problem 3 in Project Euler:

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

What is the largest prime factor of the number 600851475143 ?

This is my code:

import java.util.ArrayList;

public class Test {
    public static void main(String[] args){
        long max = 600851475143L;
        ArrayList<Long> primes = new ArrayList<>();
        primes.add((long) 2);
        boolean prime = true;
        for (long i = 3; i <= max; i += 2){
            for (long j = 3; j < Math.sqrt(i); j++){
                if (i % j == 0){
                    prime = false;
                    break;
                }
            }
            if (prime) primes.add(i);
            else prime = true;
        }
        for (int i = primes.size() - 1; i >= 0; i--){
            if (max % primes.get(i) == 0){
                System.out.println(primes.get(i));
                return;
            }
        }
    }
}

The code is not outputting anything, it just gives me a blank screen. Please do not solve the problem for me, just tell me what the bug is that is preventing it from outputting anything.

Upvotes: 1

Views: 106

Answers (2)

WJS
WJS

Reputation: 40062

You are wasting time calculating all the primes when you don't have too.

  • When you find the first prime, try reducing max by that prime until it is not longer divisible.
  • Then continue finding the next prime.
  • and reducing max by factoring out that prime.
  • each time check to see if max is equal to the current prime. If so, you are done.

Assuming you are finding primes correctly (which I believe you are) consider the following:

primes = 2,3,5,7,11,13
max = 99

is 99 divisible by 2 - no, try next prime.
is 99 divisible y  3 -  yes
max = 33
is 33 divisble by 3  - yes 
max = 11
is 11 divisible by 3 - no
by 5 - no
by 7 - no
by 11 - hey, max is a prime! And it must be the largest because
it can't be reduced anymore.

And if you want, when finding each prime factor of max, save it in a list.

Then multiply all the values in the list to see if the product == max.

Here is your code

import java.util.ArrayList;

public class Test {
    public static void main(String[] args){
        long max = 600851475143L;
          // right here, reduce max by current prime (which starts at 2)

        for (long i = 3; i <= max; i += 2){
            boolean prime = true;
            for (long j = 3; j < Math.sqrt(i); j++){
                if (i % j == 0){
                    prime = false;
                    break;
                }
            }
            if (prime)  {
            // right here, reduce max by current prime

            }
        }
    }
}

Upvotes: 3

csteel
csteel

Reputation: 468

Are you sure your program is completing? Iadded the following code below and it looks like the first for loop is going to take a long time to complete, which may be why you aren't seeing any output. To see your progress, try adding in a print statement like below:

import java.util.ArrayList;

public class Test {

    public static void main(String[] args){
        long max = 600851475143L;
        ArrayList<Long> primes = new ArrayList<Long>();
        primes.add((long) 2);
        boolean prime = true;
        for (long i = 3; i <= max; i += 2){
            if(i % 1000005 == 0)
                System.out.println("i = " + i);
            for (long j = 3; j < Math.sqrt(i); j++){
                if (i % j == 0){
                    prime = false;
                    break;
                }
            }
            if (prime) primes.add(i);
            else prime = true;
        }
        for (int i = primes.size() - 1; i >= 0; i--){
            if (max % primes.get(i) == 0){
                System.out.println(primes.get(i));
                return;
            }
        }
    }
}

Upvotes: 2

Related Questions