1

我有一个检查素数的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();
        }
    }
}

这是我的主要 void 方法:

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();
    }
}
4

4 回答 4

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");
}
于 2013-10-20T22:01:45.460 回答
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);
    }
}
于 2014-04-05T18:56:37.590 回答
0

查看http://rosettacode.org/wiki/Miller-Rabin_primality_test#Java,它解释了如何确定一个数字是否是素数。

然后处理您的值数组并保留一个数字是否为素数的标志。

于 2013-10-20T22:14:27.473 回答
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();
    }   
}

}

于 2013-10-21T08:53:06.963 回答