Java 中的素数(return 下一个素数为 false)
Prime Numbers in Java (return next prime when false)
public class PrimeNumbers {
public static void main(String[] args) {
int num = 6;
boolean isPrime = true;
for (int i = 2; i <= num - i; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
int nextPrime = num++;
{
while (isPrime = false) {
}
if (isPrime)
System.out.println("Is a prime number");
else
System.out.println("Is not a prime number" + "the next prime number is" + nextPrime(num));
}
}
}
我曾尝试在 Java 中编码以找到下一个质数,而之前返回的是假数,但我真的很困惑如何继续“while”代码。这是我到目前为止尝试过的方法:
为了找到质数,您基本上需要一个嵌套循环。所以我建议在 while 循环中使用 for 循环。或者你可以在外面有一个 do-while 循环,一直循环直到你找到一个素数。
您的代码应如下所示:
public class PrimeNumbers {
public static boolean check_prime(int num){
for (int i = 2; i <= num-i ; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args){
int num = 6;
boolean isPrime= true;
if(!check_prime(6)){
int nextPrime = num;
do{
nextPrime++;
if (check_prime(nextPrime)) {
isPrime = true;
break;
}else{
isPrime = false;
}
}while(!isPrime);
}
if (isPrime)
System.out.println("Is a prime number");
else
System.out.println("Is not a prime number" +"the next prime number is" + nextPrime);
}}}
您可以创建两个函数:一个(例如,isPrime
)检查数字是否为素数,第二个(例如,nextPrime
)查找下一个素数。第二个函数中的逻辑将非常简单:继续将数字递增 1
直到 isPrime
returns true
.
请注意,您只需检查数字的平方根即可确定它是否为质数。检查 https://en.wikipedia.org/wiki/Primality_test 以了解更多信息。
演示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = scanner.nextInt();
if (isPrime(n)) {
System.out.println("It is a prime number");
} else {
System.out.println("It is not a prime number. The next prime number is " + nextPrime(n));
}
}
static boolean isPrime(int n) {
if (n == 1 || n == 0) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
static int nextPrime(int n) {
while (!isPrime(n)) {
n++;
}
return n;
}
}
样本运行:
Enter an integer: 10
It is not a prime number. The next prime number is 11
另一个样本运行:
Enter an integer: 11
It is a prime nmber
这段代码怎么样?
public static void main(String[] args) {
int number= 14; // will print the second SOUT statement with 17
if (isPrime(number)) {
System.out.println(number+ " is a prime number");
} else {
System.out.println(number+ " is not a prime number, " +
"the next prime number is " + nextPrime(number));
}
}
/**
* Check if the number is a prime number
* For 0 or 1 you can return false, for the rest if is divisible by each number
* You might want to throw an exception for the negative input
*/
static boolean isPrime(int number) {
if (number==0 || number==1) {
return false;
}
for (int i=2; i<=number/2; i++) {
if (number%i==0) {
return false;
}
}
return true;
}
/**
* Iterate the number by one until you find a prime number using the while cycle
*/
static int nextPrime(int number) {
int prime;
while (true) {
boolean isPrime = isPrime(++number);
if (isPrime) {
prime = number;
break;
}
}
return prime;
}
public class PrimeNumbers {
public static void main(String[] args) {
int num = 6;
boolean isPrime = true;
for (int i = 2; i <= num - i; i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
int nextPrime = num++;
{
while (isPrime = false) {
}
if (isPrime)
System.out.println("Is a prime number");
else
System.out.println("Is not a prime number" + "the next prime number is" + nextPrime(num));
}
}
}
我曾尝试在 Java 中编码以找到下一个质数,而之前返回的是假数,但我真的很困惑如何继续“while”代码。这是我到目前为止尝试过的方法:
为了找到质数,您基本上需要一个嵌套循环。所以我建议在 while 循环中使用 for 循环。或者你可以在外面有一个 do-while 循环,一直循环直到你找到一个素数。
您的代码应如下所示:
public class PrimeNumbers {
public static boolean check_prime(int num){
for (int i = 2; i <= num-i ; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
public static void main(String[] args){
int num = 6;
boolean isPrime= true;
if(!check_prime(6)){
int nextPrime = num;
do{
nextPrime++;
if (check_prime(nextPrime)) {
isPrime = true;
break;
}else{
isPrime = false;
}
}while(!isPrime);
}
if (isPrime)
System.out.println("Is a prime number");
else
System.out.println("Is not a prime number" +"the next prime number is" + nextPrime);
}}}
您可以创建两个函数:一个(例如,isPrime
)检查数字是否为素数,第二个(例如,nextPrime
)查找下一个素数。第二个函数中的逻辑将非常简单:继续将数字递增 1
直到 isPrime
returns true
.
请注意,您只需检查数字的平方根即可确定它是否为质数。检查 https://en.wikipedia.org/wiki/Primality_test 以了解更多信息。
演示:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an integer: ");
int n = scanner.nextInt();
if (isPrime(n)) {
System.out.println("It is a prime number");
} else {
System.out.println("It is not a prime number. The next prime number is " + nextPrime(n));
}
}
static boolean isPrime(int n) {
if (n == 1 || n == 0) {
return false;
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
static int nextPrime(int n) {
while (!isPrime(n)) {
n++;
}
return n;
}
}
样本运行:
Enter an integer: 10
It is not a prime number. The next prime number is 11
另一个样本运行:
Enter an integer: 11
It is a prime nmber
这段代码怎么样?
public static void main(String[] args) {
int number= 14; // will print the second SOUT statement with 17
if (isPrime(number)) {
System.out.println(number+ " is a prime number");
} else {
System.out.println(number+ " is not a prime number, " +
"the next prime number is " + nextPrime(number));
}
}
/**
* Check if the number is a prime number
* For 0 or 1 you can return false, for the rest if is divisible by each number
* You might want to throw an exception for the negative input
*/
static boolean isPrime(int number) {
if (number==0 || number==1) {
return false;
}
for (int i=2; i<=number/2; i++) {
if (number%i==0) {
return false;
}
}
return true;
}
/**
* Iterate the number by one until you find a prime number using the while cycle
*/
static int nextPrime(int number) {
int prime;
while (true) {
boolean isPrime = isPrime(++number);
if (isPrime) {
prime = number;
break;
}
}
return prime;
}