简体   繁体   中英

Java Prime Factorization: Program timing out?

Before I get blasted for not following the rules, I DID utilize the search function and see that there are multiple threads on this exact problem. However, none of them answered my specific question.

I'm working on Euler problem #3, where I need to find the highest prime factor of 600851475143. I don't need help solving the problem. I have made a brute force method (could be better, I know) for solving it.

The program returns correctly for all of the tests that I did with smaller numbers (7 digits and less). However, when I enter 600851475143 as a long input, my program never gives me a return. Is my number simply too big the be entered? What could be causing this to happen? I originally thought it was because I was using int tags instead of long, but changing those didn't alter my result.

I'm sure this is simple and I'm missing it, but I am very curious as to what's happening. Thank you in advance :)

//Euler 3: Largest Prime Factor
import java.io.*;
import java.util.Scanner;
import java.lang.Math;


public class Euler3 
{
    public static void main(String[] args)
    {
        Scanner scn = new Scanner(System.in);

        System.out.println("Enter a number!");
        // Create scanner
        long numberInput=scn.nextLong();
        //Can't have a factor higher than it's square root
        double limit=Math.floor(Math.sqrt(numberInput));
        // System.out.println(limit);

        //Start testing from the highest number possible
        for(long i=(numberInput-1);i>0; i--)
        {
            if(numberInput%i==0) 
                System.out.println(i+" is prime: "+isPrime(i));

        }

    } //End Main


    public static boolean isPrime(long n) 
    {
        //check if n is a multiple of 2
        if (n%2==0) return false;
        //if not, then just check the odds
        for(int i=3;i*i<=n;i+=2)
        {
            if(n%i==0)
                return false;
        }
        return true;
    }
}   

To verify if a number is Prime or not, use the Sieve of Eratosthenes , it will run much faster compared to your naive isPrime method. I won't provide any implementation hint due to this phrase: I don't need help solving the problem .

Also, there could be other hints that you missed. I recommend you to review the problem statement:

What is the largest prime factor

A long variable can easily fit your desired value into it

long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.

The problem is that your algorithm is not an efficient one. As has already been mentioned, use the Sieve of Eratosthenes

The problem is not in the isPrime method but in the loop you use to call it. You count down from your number n with increments of 1, and you call isPrime only for divisors of n : it will take n/2 steps before you get to the first potential divisor of n - so, in the order of 10^12 steps for your example n .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM