这是我为在上限可以大到1000000000的范围之间找到素数而编写的代码。我使用了Hashmap并且没有存储任何偶数,除了2并且没有存储sero和一个也是。但是当我使用输入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();
}
}
答案 0 :(得分:4)
将您的算法转换为使用BitSet代替,您将看到巨大的性能和内存使用改进。
如果你四处搜索,你会发现这个算法的很多变种。例如,您可以看一下:http://www.dreamincode.net/forums/topic/192554-secret-code-vii-prime-numbers/
答案 1 :(得分: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';
}
}
}
答案 2 :(得分:0)
你可以在找到后输入primenumber。所以你不需要更多的记忆。 您可以使用此选项运行代码。 -Xms512M -Xmx2048M