简体   繁体   English

Java Prime Factorization:程序超时了吗?

[英]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. 在我因不遵守规则而受到抨击之前,我DID利用搜索功能,发现在这个确切的问题上有多个线程。 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. 我正在研究Euler问题#3,在那里我需要找到最高的主要因子600851475143。 我不需要帮助解决问题。 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). 对于我使用较小数字(7位以下)进行的所有测试,程序均会正确返回。 However, when I enter 600851475143 as a long input, my program never gives me a return. 但是,当我输入600851475143作为长输入时,程序永远不会给我返回。 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. 我最初以为是因为我使用的是int标签而不是long标签,但是更改这些标签并不会改变我的结果。

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. 要验证数字是否为质数,请使用Eratosthenes筛网 ,它比您朴素的isPrime方法运行得快得多。 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变量可以很容易地将您想要的值放入其中

long: The long data type is a 64-bit signed two's complement integer. long:long数据类型是一个64位带符号的二进制补码整数。 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). 最小值为-9,223,372,036,854,775,808,最大值为9,223,372,036,854,775,807(含)。 Use this data type when you need a range of values wider than those provided by int. 当需要的值范围比int提供的值宽时,请使用此数据类型。

The problem is that your algorithm is not an efficient one. 问题在于您的算法效率不高。 As has already been mentioned, use the Sieve of Eratosthenes 如前所述,使用Eratosthenes筛

The problem is not in the isPrime method but in the loop you use to call it. 问题不在于isPrime方法中,而在于您用来调用它的循环中。 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 . 您从数字n递减1递增计数,并且仅对n除数调用isPrime :到达n的第一个可能的除数之前将需要n/2步-因此,顺序为10 ^ 12步您例如n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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