【发布时间】:2019-06-19 09:24:15
【问题描述】:
我已经返回代码(在 Java 中)以找到给定数字的最大素因子。
我发现检查了所有因素,然后检查它是否是素数……如果是,则打印最大的素数。
import java.util.Scanner;
public class Problem3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int n = Integer.parseInt(sc.next()); // Takes input from the user.
int p=0,i,j,max=0,c=0;
for(i=1;i<n;i++) {
if(n%i != 0) { //Checks for factors and assigns that value to "c"
c = i;
for(j=1;j<c;j++) {
if(c%j==0) { //checks for prime number or not, if so... assign that value to "p"
p = j;
}
if(max<p) { // Checks for largest Prime factors, and assigns that value to "max"
max = p;
}
}
}
}
System.out.println(max); // prints the maximum prime-factor value.
sc.close();
}
}
我预计14的输出是7,但实际输出是1
【问题讨论】:
-
我尝试输入“13195”,我得到“2639”作为输出....但是 2639 不是素数!