在java中测试素数

时间:2012-03-23 21:36:57

标签: java

我的程序的目标是编写一个程序,提示用户输入一个整数。程序将读取整数并确定它是否是素数。如果它不是素数,它会告诉用户所有的除数。

这是我有多远,但当我用169或289这样的数字进行测试时,程序说他们是素数。我明白问题就在于这条线:

int[] div = new int[] { 2,3,4,5,6,7,8,9};

我试着这样做:

for (int s = nr; s != 0 ; s--) { 
      if (nr%s == 0) {
      int[] div = new int[]{s}; }

但它没有用。一点帮助或正确的方向会有很大帮助。谢谢!

public static void main(String[] args){
    System.out.println("enter a number:");
    Scanner scanner = new Scanner (System.in);
    int nr = scanner.nextInt();
    int[] div = new int[] { 2,3,4,5,6,7,8,9}; 
    boolean prime = nr >= 2;
    int i = nr;
    for(int j = 0; j< div.length && prime && i> div[j]; j++)
        if(i % div[j] == 0)
            prime = false;

    if(prime){
        System.out.println(i + " is a prime");
    }else{
        System.out.print(i + " is divisible");
        for(int k = 2; k < i; k++)
            if(i % k == 0){
                System.out.print( k);
                System.out.print(",");}

                }

            } }

5 个答案:

答案 0 :(得分:5)

对于除数,你只会尝试高达9的数字。所以任何有10或更高因子的东西,比如169比13比13,你都找不到13分。

不要将除数存储在数组中,而应考虑使用整数并向上计数。也就是说,代替div[j]只使用j作为你的除数,并且不要让它在10处停止。让它停在最高可能的除数(这是你正在尝试的数字的平方根)找到素数。

答案 1 :(得分:5)

<edit> I should have looked a little closer... I guess it IS homework, so this answer is not really any good to you, eh? Well, I'm gonna leave it up in case the information here becomes useful to someone somewhere... </edit>

如果您实际上不需要自己进行计算(如果这不是家庭作业,那么),您可以选择使用Java的BigInteger类。例如:

public class Prime { 
    public static void main(String[] args){
        long start = System.nanoTime();
        System.out.println("enter a number:");
        Scanner scanner = new Scanner(System.in);
        BigInteger bigInt = scanner.nextBigInteger();
        boolean prime = bigInt.isProbablePrime(10);
        System.out.println(prime);
    }
}

与生活中的大多数事情一样,至少有一些警告:

  1. 报告数字素数的概率不是100%,也不是(但我们马上就会谈到这一点);以及

  2. 您希望测试的准确度越高,所需的时间就越长。

  3. 详见Oracle's Official Documentation

      

    <强> isProbablePrime

         

    public boolean isProbablePrime (int certainty)

         

    如果此BigInteger可能是素数,则返回true,如果它肯定是复合的,则返回false。如果certainty<= 0,则会返回true

         

    <强>参数:

         

    certainty - 衡量调用者愿意容忍的不确定性的指标:如果调用返回true,则此BigInteger为素数的概率超过(1 - 1/(2^certainty))。此方法的执行时间与此参数的值成比例。

         

    <强>返回:

         

    true如果这个BigInteger可能是素数,false如果它肯定是复合的。

    我很好奇它的准确程度以及高度准确的素性测试花了多少时间,因此我做了一个快速的基准并运行了一些数字。

    我为1到50之间的每个确定性值计算了每个奇数以下的每个奇数的素数(0或更小总是返回true)。

    时间以毫秒为单位(虽然时间是通过调用System.nanoTime()获得的,然后四舍五入到最接近的毫秒数。)

    以下是我的结果:

    Certainty       Time(ms)        Chance of Correctness
    1               1417            50.0%
    2               932             75.0%
    3               1064            87.5%
    4               1063            93.75%
    5               1183            96.875%
    6               1195            98.4375%
    7               1313            99.21875%
    8               1308            99.609375%
    9               1441            99.8046875%
    10              1443            99.90234375%
    11              1567            99.951171875%
    12              1571            99.9755859375%
    13              1690            99.98779296875%
    14              1691            99.993896484375%
    15              1817            99.9969482421875%
    16              1822            99.99847412109375%
    17              1944            99.99923706054688%
    18              1941            99.99961853027344%
    19              2069            99.99980926513672%
    20              2073            99.99990463256836%
    21              2197            99.99995231628418%
    22              2200            99.99997615814209%
    23              2324            99.99998807907104%
    24              2340            99.99999403953552%
    25              2453            99.99999701976776%
    26              2465            99.99999850988388%
    27              2647            99.99999925494194%
    28              2626            99.99999962747097%
    29              2715            99.99999981373549%
    30              2710            99.99999990686774%
    31              2844            99.99999995343387%
    32              2818            99.99999997671694%
    33              2950            99.99999998835847%
    34              3074            99.99999999417923%
    35              3121            99.99999999708962%
    36              3167            99.99999999854481%
    37              3295            99.9999999992724%
    38              3302            99.9999999996362%
    39              3334            99.9999999998181%
    40              3360            99.99999999990905%
    41              3493            99.99999999995453%
    42              3583            99.99999999997726%
    43              3663            99.99999999998863%
    44              3599            99.99999999999432%
    45              3783            99.99999999999716%
    46              3816            99.99999999999858%
    47              3964            99.99999999999929%
    48              3898            99.99999999999964%
    49              4029            99.99999999999982%
    50              3969            99.99999999999991%
    total: 124312
    

    正如您所看到的,即使在我测试的最高确定性值,评估500,000个数字的素数只需要4秒,每个评估的正确率为99.99999999999991%

    因此,如果您的预期应用程序不具有令人难以置信的性能关键性,您可以使用(相对)高数字,例如25.该算法将正确地将数字报告为99.99999701976776%的时间。如果你是一名火箭科学家,不推荐。如果你 ,是一名火箭科学家,那么我希望你能在其他地方找到你的解决方案:)。

答案 2 :(得分:3)

你得到169作为素数的原因是因为你只是除以值2-9而169是13的平方所以它不能被任何数字2-9整除。

您可以使用此方法http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes查找1和n之间的所有素数,并使用它来确定您的数字是否为素数。

答案 3 :(得分:1)

查找数字是否为素数的一种非常简单和基本的方法是:

继续将n除以2和n ^ 1/2之间的每个数字。如果它们中的任何一个均匀分配,则n不是素数因为你找到了一个因子。如果n没有小于其平方根的因子,那么n是素数。只检查小于或等于n ^ 1/2的除数就足够了,因为如果n = a * b,则a和b不能都超过n的平方根。

for(int i=2;i<=n^1/2;i++) 
  {
   if(num%i)==0 //number not prime
    else continue;
  }

  if none of the if statements in the loop were true, number is prime

答案 4 :(得分:0)

只需使用Java内置的BigInteger.isProbablePrime()库函数...不是100%确定如何使用确定性参数。

BigInteger.valueOf(13).isProbablePrime(10000)
相关问题