在不使用内置函数的情况下检查java中大值的素数。

时间:2022-08-24 16:28:35

I want to write a java program to check prime number for large values. I had written this code, but there are errors in my code. Please help me in solving that so that it works fine for large values.

我想编写一个java程序来检查大值的素数。我编写了这段代码,但我的代码中有错误。请帮助我解决这个问题,以便它适用于大值。

Also, is my approach right for checking prime number for large numbers without built-in functions?

另外,我的方法是否适合在没有内置函数的情况下检查大数字的素数?

Other possible solutions/approaches, please.

请提供其他可能的解决方案/方法。

My_Code:

My_Code:

import java.math.*;
import java.util.Scanner;
public class CheckPrimeNumber {
    public static void main(String[] args) 
    {
        int flag=0;
        BigInteger input;
        try{
        Scanner sc= new Scanner(System.in);
        System.out.println("Enter a valid positive number: ");
        String strinput=sc.nextLine();
        input = new BigInteger(strinput);

        sc.close();
        if(input.equals(0) ||input.equals(1)){                      
          System.out.println(input+" is not a prime number.");
            }
        else{
            for(BigInteger i=2; i < input.divide(2); i++){  
                if(input.remainder(2) == 0){
                    System.out.println(input+" is not a prime number.");
                    flag=1;
                    break;                      
                    }
                }
                if(flag==0)
                    System.out.println(input +" is a prime number.");               
            }
        }
    catch(Exception e){
        System.out.println("Please enter only valid positive number: ");
        }
    finally{
        System.out.println("Thank you...!!!");      
        }
    }
}

2 个解决方案

#1


0  

One possible approach:

一种可能的方法:

  1. Check divisibility by small primes up to, say, 5,000. This finds the easy ones.

    用小素数检查可分性,比如5000。这找到了容易的。

  2. Optionally, run a single Fermat test, base 2. This will eliminate some more composites at less cost than a Miller-Rabin test.

    可选地,运行单个Fermat测试,基础2.这将比Miller-Rabin测试以更低的成本消除更多的复合材料。

  3. If the number is not yet flagged as composite then run repeated Miller-Rabin tests to the level of accuracy you need. If you run 64 tests then the chances of an error are less than 1 in 2^128.

    如果该数字尚未标记为复合,则将重复的Miller-Rabin测试运行到您需要的准确度。如果运行64次测试,那么错误的几率在2 ^ 128中小于1。

You might also want to look at a copy of Crandall and Pomerance which is very useful in this area.

您可能还想查看Crandall和Pomerance的副本,这在该领域非常有用。

#2


-1  

First, you should post your error.

首先,你应该发布你的错误。

Your approach should be checking input/2 numbers to see whether they are divisible by some numbers. It's right but it's not the best. Check other prime algorithms for better performance.

你的方法应该检查输入/ 2号码,看看它们是否可以被某些数字整除。这是对的但不是最好的。检查其他主要算法以获得更好的性能

In the for loop, you should check input.remainder(i) instead of input.remainder(2).

在for循环中,您应该检查input.remainder(i)而不是input.remainder(2)。

#1


0  

One possible approach:

一种可能的方法:

  1. Check divisibility by small primes up to, say, 5,000. This finds the easy ones.

    用小素数检查可分性,比如5000。这找到了容易的。

  2. Optionally, run a single Fermat test, base 2. This will eliminate some more composites at less cost than a Miller-Rabin test.

    可选地,运行单个Fermat测试,基础2.这将比Miller-Rabin测试以更低的成本消除更多的复合材料。

  3. If the number is not yet flagged as composite then run repeated Miller-Rabin tests to the level of accuracy you need. If you run 64 tests then the chances of an error are less than 1 in 2^128.

    如果该数字尚未标记为复合,则将重复的Miller-Rabin测试运行到您需要的准确度。如果运行64次测试,那么错误的几率在2 ^ 128中小于1。

You might also want to look at a copy of Crandall and Pomerance which is very useful in this area.

您可能还想查看Crandall和Pomerance的副本,这在该领域非常有用。

#2


-1  

First, you should post your error.

首先,你应该发布你的错误。

Your approach should be checking input/2 numbers to see whether they are divisible by some numbers. It's right but it's not the best. Check other prime algorithms for better performance.

你的方法应该检查输入/ 2号码,看看它们是否可以被某些数字整除。这是对的但不是最好的。检查其他主要算法以获得更好的性能

In the for loop, you should check input.remainder(i) instead of input.remainder(2).

在for循环中,您应该检查input.remainder(i)而不是input.remainder(2)。