JAVA程序找到最大素数因子,但输出是否错误?
我返回了代码(Java)以查找给定数字的最大素数因子 我已经找到并检查了所有的因素,然后检查了它是否为素数。。。。如果是,请打印最大的素数因子JAVA程序找到最大素数因子,但输出是否错误?,java,primes,factors,Java,Primes,Factors,我返回了代码(Java)以查找给定数字的最大素数因子 我已经找到并检查了所有的因素,然后检查了它是否为素数。。。。如果是,请打印最大的素数因子 import java.util.Scanner; public class Problem3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter t
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();
}
}
import java.util.Scanner;
公共类问题3{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入编号”);
int n=Integer.parseInt(sc.next());//接受用户的输入。
int p=0,i,j,max=0,c=0;
对于(i=1;i您的素数检查是错误的。c
仅当c%j!=0
对于所有1
int max=0,c=0;
for(int i=1;i<n;i++) {
if(n%i == 0) { //Checks for factors and assigns that value to "c"
c = i;
for(int j=2;j<c;j++) {
if(c%j==0) { // not prime
c = 0;
break;
}
}
if(max < c) { // if c > max, it must be > 0, which means it must be prime
max = c;
}
}
}
System.out.println(max); // prints the maximum prime-factor value.
int max=0,c=0;
对于(inti=1;i0),这意味着它必须是素数
max=c;
}
}
}
System.out.println(max);//打印最大素数因子值。
您的for循环中似乎有错误。我已使用java 8为此创建了一个方法。请尝试下面的代码
Java8
publicstaticvoidmain(字符串[]args){
try(Scanner sc=新扫描仪(System.in);){
System.out.println(“输入编号”);
int n=Integer.parseInt(sc.next());//接受用户的输入。
int max=IntStream.range(0,n).filter(q->isFactor(n,q)).filter(q->isPrimeNumber(q)).max().getAsInt();
System.out.println(max);//打印最大素数因子值。
}捕获(例外e){
//TODO:处理异常
}
}
公共静态布尔isPrimeNumber(int n){
int i,m=0,flag=0;
m=n/2;
如果(n==0 | | n==1){
返回false;
}否则{
对于(i=2;i=max){//检查max number
max=i;
}
}
}
}
System.out.println(max);//打印最大素数因子值。
}捕获(例外e){
//TODO:处理异常
}
}
试试这段代码
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;
if (isPrime(c) && c > max) {
max = c;
}
}
}
System.out.println(max); // prints the maximum prime-factor value.
sc.close();
}
private static boolean isPrime(int num) {
if (num == 0 || num == 1) {
return false;
}
for (int i = 2; i < num / 2; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
publicstaticvoidmain(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入编号”);
int n=Integer.parseInt(sc.next());//接受用户的输入。
int p=0,i,j,max=0,c=0;
对于(i=1;imax){
max=c;
}
}
}
System.out.println(max);//打印最大素数因子值。
sc.close();
}
私有静态布尔值isPrime(int num){
如果(num==0 | | num==1){
返回false;
}
for(int i=2;i
谢谢,我没有看到“基本代码”,但答案还是错了……我的“14”得到了“6”。@pop我用我的循环替换了你的循环,输入14得到了输出7。谢谢,输入14得到了输出7,但另一个输入“30”,根据我的问题,我必须得到“5”(因为这是最大的基本因子),但输出是“15”,这不是质数。@pop我得到的30分是5。我不知道你在运行哪个代码,但它不可能是这个答案中的代码。这不符合要求:它会找到一个给定限制下的最大质数。这并不意味着它也是该限制的一个因素。例如,对于9
,它会找到7
,而正确的答案是be3
。我犯了一个错误。现在我修复了它。@pop In here intStream充当for循环。它将循环0到n次。n是输入值。然后在for内有一个if条件检查因子。在该条件内,if有另一个if检查素数。最后max().getAsInt()
将以整数形式返回最大值。我尝试输入“13195”,结果得到“2639”作为输出…但2639不是质数!@pop我想你是用旧版本测试的。你能再测试一次吗?我在“”(类名必须改为“Main”)上再次尝试并编译为JAVA程序,但输出错误为“2639”@pop我在onlinegdb.com上检查了我的代码,它给了我预期的输出,即29。我尝试输入“13195”,结果得到“2639”作为输出…但2639不是质数!
public static void main(String[] args) {
try (Scanner sc = new Scanner(System.in);) {
System.out.println("Enter the number");
int n = Integer.parseInt(sc.next()); // Takes input from the user.
int max = 0;
for (int i = 0; i < n; i++) {
if (isFactor(n, i)) { // checks for factor
if (isPrimeNumber(i)) { // checks for prime
if (i >= max) { // checks for max number
max = i;
}
}
}
}
System.out.println(max); // prints the maximum prime-factor value.
} catch (Exception e) {
// TODO: handle exception
}
}
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;
if (isPrime(c) && c > max) {
max = c;
}
}
}
System.out.println(max); // prints the maximum prime-factor value.
sc.close();
}
private static boolean isPrime(int num) {
if (num == 0 || num == 1) {
return false;
}
for (int i = 2; i < num / 2; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}