我有一个检查素数的java代码,然后显示它们。但是,有些情况下没有素数(例如14-17之间)。在这些情况下,我想要显示一条消息。例如"No primes found"
。我不知道如何将其添加到我的代码中。
这是我的全班:
import java.io.*;
public class Prime {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
public static int lowerBound;
public static int higherBound;
public void getInput() throws IOException {
System.out.println("Please enter the lower and upper bound");
String line1 = input.readLine();
String line2 = input.readLine();
int lowInput = Integer.parseInt(line1);
int highInput = Integer.parseInt(line2);
lowerBound = lowInput;
higherBound = highInput;
}
public void validatedata() throws IOException {
do {
getInput();
if (lowerBound < 2) {
System.out.println("Finish");
break;
} else if (higherBound < lowerBound) {
System.out
.println("The upper bound should be at least as big as the lower bound.");
}
} while (higherBound < lowerBound);
}
public void prime_calculation() throws IOException {
while ((lowerBound >= 2) && (higherBound > lowerBound)) {
int k;
for (k = lowerBound; k < higherBound; k++) {
boolean primecheck = true;
for (int j = 2; j < k; j++) {
if (k % j == 0) {
primecheck = false;
}
}
if (primecheck)
System.out.println(k);
}
validatedata();
}
}
}
这是我的主要无效方法:
import java.io.IOException;
public class PrimeUser extends Prime {
public static void main(String argv[]) throws IOException {
Prime isprime = new Prime();
isprime.validatedata();
isprime.prime_calculation();
}
}
答案 0 :(得分:2)
ArrayList<Integer> primes = new ArrayList<Integer>();
if ((lowerBound >= 2) && (higherBound > lowerBound))
{
int k;
for (k = lowerBound; k < higherBound; k++)
{
primecheck = true;
for (int j=2; j < k/2; j++) // you dont have to check beyond k/2, since (k/2 + 1) times anything whole number cannot be equal to k
{
if (k % j == 0) {
primecheck = false;
break; // no need for more iterations of the current number, since you already know it is not a prime
}
}
if (primecheck) {
primes.add(k);
}
}
} else {
// invalid bounds
return;
}
if (primes.size() > 0) {
for (int num : primes) {
System.out.println(num);
}
} else {
System.out.println("No primes exist");
}
// Without an array
boolean y = false;
String x = new String();
if ((lowerBound >= 2) && (higherBound > lowerBound))
{
int k;
for (k = lowerBound; k < higherBound; k++)
{
primecheck = true;
for (int j=2; j < k/2; j++) // you dont have to check beyond k/2, since (k/2 + 1) times anything whole number cannot be equal to k
{
if (k % j == 0) {
primecheck = false;
break; // no need for more iterations of the current number, since you already know it is not a prime
}
}
if (primecheck) {
x += Integer.toString(k) + "\n";
y = true; // yes, there exists atleast 1 prime
}
}
} else {
// invalid bounds
return;
}
if (y == true) {
System.out.println(x);
} else {
System.out.println("No primes exist");
}
答案 1 :(得分:1)
假设2和3是已知的素数: 将在不到900毫秒的时间内找到100万个素数
公共类FindPrime { private Integer [] arr_num = new Integer [1000000];
private void arr_init()
{
for(int i=0; i< arr_num.length; i++)
{
arr_num[i] = i;
}
}
private void find_prime()
{
for (int i=3; i<arr_num.length; i++)
{
find_prm(arr_num[i]);
}
}
private void find_prm(Integer chk_prm)
{
boolean isprime = false;
if(chk_prm == null)
return;
if(chk_prm %2 !=0 )
{
double sqrt = Math.sqrt(chk_prm);
for(Integer k=arr_num[2]; (k!= null) && (k<sqrt + 1) ; k++)
{
if(chk_prm % k == 0)
{
isprime=false;
break;
}
else
isprime = true;
}
}
else
{
setNull(chk_prm);
}
if(!isprime)
{
setNull(chk_prm);
}
}
private void setNull(int num_to_null)
{
arr_num [num_to_null] = null;
}
private void print_prime()
{
for(int i=2; i < arr_num.length; i++)
{
if(null != arr_num[i])
{
System.out.println(arr_num[i]);
}
}
}
public static void main(String [] args)
{
FindPrime findPrime = new FindPrime();
findPrime.arr_init();
long timeStart = System.currentTimeMillis();
findPrime.find_prime();
long diff = System.currentTimeMillis()- timeStart;
System.out.println("Running time :: " + diff);
}
}
答案 2 :(得分:0)
查看http://rosettacode.org/wiki/Miller-Rabin_primality_test#Java,了解如何确定数字是否为素数。
然后处理你的数组值并保持一个数字是否是一个素数。
答案 3 :(得分:0)
我找到了解决问题的简便方法。我只是添加了一个计数器来计算出现次数 素数。如果计数器等于= 0,则显示消息。代码如下。不管怎样,谢谢你的帮助。如果我有任何问题,我会再次发布。顺便说一句,我想知道是否有人可以告诉我或分享任何来源/网页,或者只是以任何方式训练我的算法技巧来寻找解决方案?
这里的最终代码
public void prime_calculation() throws IOException
{
while ((lowerBound >= 2) && (higherBound > lowerBound))
{
int k;
int m = 0;
for (k = lowerBound; k < higherBound; k++)
{
boolean primecheck = true;
for (int j=2; j < k; j++)
{
if (k % j == 0)
{
primecheck = false;
}
}
if (primecheck)
{
System.out.println(k);
m++;
}
}
if (m == 0)
System.out.println("No primes found");
validatedata();
}
}
}