问题描述
因此,当前,我正在编写一段代码,该代码将确定整数是否是质数,并且由于我已使用不同的质数和非质数整数对其进行了多次测试,因此效果很好。现在,我必须通过列出所有给定整数的所有因子来进行总结。该程序按原样工作。
任何对此的帮助将不胜感激。谢谢!
import java.util.*;
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("What is your number?");
int # = console.nextInt();
int div = 2;
boolean run = false;
System.out.println("The factors of " + # + " are:");
while (div <= Math.sqrt(#)) {
if (# % div == 0) {
System.out.println(div + "," + #/div);
div++;
run = true;
} else {
div++;
}
}
if (run == false) {
System.out.print("This is a prime number!");
}
}
}
解决方法
对于初学者来说,您可以使用此:
Scanner console = new Scanner(System.in);
int num = console.nextInt();
int factor = 1;
while(factor<=num) {
if(num%factor==0) {
System.out.println("Found factor: "+factor);
}
factor++;
}
console.close();
那是什么呢?
您想要做的是遍历从1一直到该数字的每个数字。在while循环while(factor<=num)
中的语句中对此进行了说明。
要检查它是否是一个因素,我们可以使用语句if(num%factor==0)
。这是什么意思? %
表示模数,基本上,它会在将一个数字除以另一个数字后返回余数。如果余数为0,则说明它是一个因子(可以成功除法。如果您不知道因子是什么,建议您首先学习数学)。
然后我们每次将factor
加1,并检查每个小于用户输入的数字,因为没有使用检查较大的数字(如果数字较大,则它永远不会成为另一个因素)。
测试运行
10
Found factor: 1
Found factor: 2
Found factor: 5
Found factor: 10
如果要实施素数验证,可以执行以下操作:
Scanner console = new Scanner(System.in);
int num = console.nextInt();
int factor = 1;
int factors=0;
while(factor<=num) {
if(num%factor==0) {
System.out.println("Found factor: "+factor);
factors++;
}
factor++;
}
if(factors==2) {
System.out.println("This is a prime number!");
}
console.close();
最终版本(根据您的要求)
Scanner console = new Scanner(System.in);
int num = console.nextInt();
int factor = 1;
int factors=0;
System.out.print("The factors of "+num+" are: ");
while(factor<=num) {
if(num%factor==0) {
System.out.print(factor+" ");
factors++;
}
factor++;
}
if(factors==2) {
System.out.println("This is a prime number!");
}
console.close();
测试用例
2
The factors of 2 are: 1 2
This is a prime number!
,
boolean isPrime = true;
for(int i = 2; i <= num/2; i++){
if(num % i == 0) {
System.out.println("The factors of " + num + " are:" + i);
isPrime = false;
}
}
if(isPrime) {
System.out.print("This is a prime number!");
}
我想让您想到的是为什么我只在num / 2而不是num之前运行循环?让我知道是否可以解决。
,稍微更改while循环以存储因素。
List<Integer> factors = new Arraylist<>();
int upperLimit = num/2
while (div <= upperLimit) {
if (num % div == 0) {
factors.add(div);
}
}
如果在循环结束时执行此操作,则作为列表的factors
变量将包含所有numebr因子。现在,您可以检查列表中是否存在除1或本身以外的数字,如果存在,则不是质数。
注意:如果您不知道Java中有哪些列表,也可以使用数组来代替列表。
,我只是对您的代码做了一些小改动。这将以最佳的时间复杂度给出答案。
import java.util.*;
public class myClass{
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("What is the number that you have chosen?");
int num = console.nextInt();
int div = 2;
boolean run = false;
System.out.println("The factors of " + num + " are:");
System.out.print("1");
while (div <= Math.sqrt(num)) {
if (num % div == 0) {
System.out.print(","+div + "," + num/div);
div++;
run = true;
} else {
div++;
}
}
System.out.print(","+num);
if (run == false) {
System.out.print("\nThis is a prime number!");
}
}
}
测试用例
What is the number that you have chosen?
The factors of 10 are:
1,2,5,10
What is the number that you have chosen?
The factors of 5 are:
1,5
This is a prime number!
希望这对您有所帮助。