C - 从特定的大范围中获取素数

标签 c arrays memory

#include <stdio.h>
#include <math.h>
#define UPPER_LIMIT 2147483647
void sieve(unsigned long int n, unsigned long int primes[]);  
main()  
{  
    unsigned long int low, up, steps;  
    unsigned long int v[UPPER_LIMIT];  
    sieve(UPPER_LIMIT, v);  
    scanf("%ld\n",&steps);  
    for (unsigned long int i=0;i<steps;i++){  
        scanf("%ld %ld\n",&low,&up);  
        for(unsigned long int j=low; j<up; j++){  
            if (v[j] == 1){  
                printf("%ld\n",j);  
            }  
        }   
    }  
}  
void sieve(unsigned long int n, unsigned long int primes[])  
{  
    for (unsigned long int i=0;i<n;i++){  
        primes[i]=1;  
    }  
    primes[0]=0,primes[1]=0;   

    for (unsigned long int i=2;i<sqrt(n);i++) {  
        for (unsigned long int j=i*i;j<n;j+=i){  
            primes[j] = 0;  
        }  
    }  
}  

我正在尝试解决打印特定范围内的素数的问题。 首先 scanf 我们得到要审查的案例数量。该范围由标准输入的下一行给出,例如 (1 10) 上限值可以是最大值 2147483647。我正在使用埃拉斯托斯特筛法来查找素数。之后我想按升序 printf 素数。不幸的是,我遇到了运行时错误,我认为这是因为我试图创建的数组非常大。我需要有关问题的可能解决方案的建议。

标准输入示例:

1  
1 10

标准输出示例:

2  
3  
7  

最佳答案

使用unsigned long是没有意义的。仅存储 0 的数组或1 .

您只需要2147483647 / 8位来存储您需要的所有信息,因此您应该声明一个最多 2147483647 / 8 + 1 的数组字节:

const unsigned int SIZE = 1 + (2147483647 / 8);
unsigned char primes[SIZE];

或者更好的是,通过堆分配:

const unsigned int SIZE = 1 + (2147483647 / 8);
unsigned char *primes = (unsigned char*)malloc(SIZE);

数组初始化变为:

for (unsigned i = 0; i < SIZE ; i++ ){  
    primes[i] = 1;
}

可以使用按位运算符 >> 来访问数组的各个位。 , << ,和& 。实现此功能将作为练习,因为这看起来像大学练习。

关于C - 从特定的大范围中获取素数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43642916/

相关文章:

c - 收到警告 : assignment from incompatible pointer type for using pointer to char array in struct

c# - 删除字符串数组开头的字符

java - 大输入数组中的奇数异或对

ios - 以编程方式检索 iPhone 上的内存使用情况

multithreading - 当内存使用率变高时 Perl 多线程变慢

c - 将非字母数字符号映射到宏

c - 累计和溢出

c - 为什么我得到 EXC_BAD_ACCESS(代码=1,地址=0x0)?

python - 如何在 Numpy 中就地扩展数组?

c# - 强制执行内存级别转换