取所有素数小于Java中的大整数

时间:2016-01-23 11:13:45

标签: java biginteger

我想将所有素数小于一个整数(例如:123456789012345)。以下代码适用于整数。但是对于大整数而言不起作用。 obj.runEratosthenesSieve(upperBound); ---这里标记为错误。如何更改为123456789012345之类的大整数。

   import java.math.BigInteger;
    public class Prime_Numbers {

                public void runEratosthenesSieve(int upperBound) {
                      int upperBoundSquareRoot = (int) Math.sqrt(upperBound);
                      boolean[] isComposite = new boolean[upperBound + 1];
                      for (int m = 2; m <= upperBoundSquareRoot; m++) {
                            if (!isComposite[m]) {
                                  System.out.print(m + " ");
                              for (int k = m * m; k <= upperBound; k += m)
                                        isComposite[k] = true;
                            }
                      }
                      for (int m = upperBoundSquareRoot; m <= upperBound; m++)
                            if (!isComposite[m])
                                  System.out.print(m + " ");
                }
  public static void main(String[] args) {
  BigInteger  upperBound = new    BigInteger("123456789012345");
  Prime_Numbers obj = new Prime_Numbers();
  obj.runEratosthenesSieve(upperBound); -------------------????
    }}

0 个答案:

没有答案