[英]Best Way to Optimize this Largest Prime Factor Program
I need some help optimizing this program.我需要一些帮助来优化这个程序。 I am trying to figure out the largest prime factor for input.我试图找出输入的最大主要因素。 However, it occasionally has timeout issues, so I am interested in figuring out how to optimize it.但是,它偶尔会出现超时问题,所以我有兴趣弄清楚如何优化它。
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class PFactor() {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
long n = in.nextLong();
System.out.println(pMod(n));
}
}
public static int pMod(long d) {
long maxVal = 0;
for (long i = 2; i <= d; i++) {
if (d % i == 0) {
boolean prime = pCount(i);
if (prime == true) {
max = i;
}
} else {
max = max;
}
}
return (int)max;
}
public static boolean pCount(long inLong) {
int count = 0;
for (long s = 1; s <= inLong; s++) {
if (inLong % s == 0) {
count++;
}
}
if (count == 2) {
return true;
} else {
return false;
}
}
}
Do you know how to optimize this code so that it does not have as many timeouts?您知道如何优化此代码以使其没有那么多超时吗? I need this ready soon for something at work, so I decided to reach out to see if I could get some help, as I can't seem to figure it out myself where it needs further optimization.我需要尽快为工作中的某些事情做好准备,所以我决定伸出手来看看我是否能得到一些帮助,因为我自己似乎无法弄清楚它需要进一步优化的地方。
I actuallly found a solution guys after a bit of tinkering with it!经过一番修改后,我实际上找到了解决方案! you were all very helpful though!不过,你们都非常有帮助! Thanks for helping!感谢您的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.