0

这是我为在上限可以大到 1000000000 的范围之间查找素数而编写的代码。我使用了 Hashmap,除了 2 之外没有存储任何偶数,也没有存储 sero 和 1。但仍然当我使用输入 lb = 1 和 ub = 1000000000 运行此代码时,它会给出运行时错误,(内存不足)。请帮助

这是我的代码:-

import java.util.HashMap;
import java.util.Iterator;

import java.util.Scanner;

class Samp {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int t, limit, m, n;
        double lb, ub, c;

        t = sc.nextInt();
        while (t > 0) {
            c = 3;
        HashMap<Integer, Boolean> primeflags = new HashMap<Integer, Boolean>();
            primeflags.put(2, true);
            lb = sc.nextDouble();
            ub = sc.nextDouble();
            while (c <= ub) {
                primeflags.put((int) c, true);
                c = c + 2;
            }
            limit = (int) Math.sqrt(ub);

            for (m = 2; m <= limit; m++) {

                for (n = m * m; n <= ub; n += m) {

                    if (primeflags.containsKey(n))
                        primeflags.remove(n);
                }

            }
            Iterator<Integer> iterator = primeflags.keySet().iterator();
            while (iterator.hasNext()) {  
                   Integer key = (Integer) iterator.next(); 

                   if(key >= lb)
                   System.out.println(key); 
                }



            --t;

        }
       sc.close();
     }    
}

好的,在收到答案后,我编码了这个:但它仍然在给 TLE

import java.util.BitSet;
import java.util.Scanner;
public class Prime {
    private static BitSet bitSet = new BitSet(1000);
    private static int max = 3;

      public static boolean isPrime(int n) {
          if(n == 2)
                      return true;
          if(n < 3 || n % 2 == 0)
               return false;
          if(n <= max)
              return !bitSet.get(n / 2);
          for(int i = 3; i <= n; i += 2) {
              if(i * 2 > n)
                 break;
              if(!bitSet.get(i / 2)) {
                  int multiple = max / i;
                  multiple *= i;
                  if(multiple <= i)
                multiple = i * 2;
                  clearMultiple(i, multiple, n);  
              }
          }
          max = n;
          return !bitSet.get(n / 2);
      }

    private static void clearMultiple(int prime, int multiple, int max) {
        while(multiple <= max) {
                    setNotPrime(multiple);
                    multiple += prime;
                   }

    }

    private static  void setNotPrime(int n) {

        if(n % 2 == 0)
             return;
              bitSet.set(n / 2, true);
}     


    public static void getPrimeGreaterOrEqual(int n,int upperbound) {
            // make sure we start with an odd number
        if( n == 1 || n == 0){
            System.out.println(2);
            n = 3;
        }    
    if(n % 2 == 0 && n != 2)
                    ++n;
                // loop until we found one
                    while(n <= upperbound) {
                //if the number is registered as prime return it
                if(isPrime(n))
                       System.out.println(n);
                       // else check next one
                        n += 2;


               }  
            }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int t,lb,ub;
         t = sc.nextInt();
        while(t > 0){
            lb = sc.nextInt();
            ub = sc.nextInt();

            getPrimeGreaterOrEqual(lb, ub);

            --t;
       }
       sc.close();
    }
}
4

3 回答 3

4

将您的算法转换为使用BitSet,您将看到巨大的性能和内存使用改进。

如果您四处搜索,您会发现该算法的许多变体。例如,您可以看看这个:http ://www.dreamincode.net/forums/topic/192554-secret-code-vii-prime-numbers/

于 2012-10-21T08:49:03.583 回答
2

我手头没有Java的实现。我用 C++ 编写了以下实现,并用它来计算高达 1.000.000.000.000 的素数:

void sieve(std::size_t bound, std::ostream& os)
{
    using uint_t = unsigned int;
    constexpr auto bits = sizeof(uint_t) * CHAR_BIT;
    auto&& index = [](std::size_t i) { return (i - 3) / 2 / bits; };
    auto&& mask = [](std::size_t i) { return uint_t{1} << ((i - 3) / 2 % bits); };
    auto size = index(bound - 1) + 1;
    std::unique_ptr<uint_t[]> crossed{new uint_t[size]};
    std::fill(crossed.get(), crossed.get() + size, uint_t{0});
    for (std::size_t i = 3; i < bound; i += 2) {
        if ((crossed[index(i)] & mask(i)) == 0) {
            auto q = i * i;
            if (q >= bound) {
                break;
            }
            for (; q < bound; q += i) {
                crossed[index(q)] |= mask(q);
            }
        }
    }
    os << "2\n";
    for (std::size_t i = 3; i < bound; i += 2) {
        if ((crossed[index(i)] & mask(i)) == 0) {
            os << i << '\n';
        }
    }
}
于 2012-10-21T08:55:15.410 回答
0

您可以在查找后放置素数。所以你不需要更多的内存。您可以使用此选项运行您的代码。 -Xms512M -Xmx2048M

于 2012-10-21T09:04:38.143 回答