c - 埃拉托斯特尼筛法,素数 C

标签 c math numbers primes

我正在编写一个程序,使用埃拉托斯特尼筛法查找 1000 之前的素数...但是它不起作用...这是我的整个代码,但是查找素数的“计算”位于函数“markPrimes”内部(我确信其余代码都没有问题,所以我很确定问题出在这个函数中......

#include <stdio.h>
#include <stdlib.h>

typedef struct primal
{
    int number; /* a number */
    char mark;  /* flag marking the number as active (1) or inactive (0) */
} primal;


void initialize(primal *s,int size)
{
    int i;
    for(i=0;i<1000;i++)
    {
        s[i].number=i+1;
        s[i].mark=1; //1=prime number 
    }
}

void markPrimes(primal *s,int size)
{
    /* add this function - it should mark all of the numbers in the passed primal array that are not prime numbers as inactive */
    //Brute Sieve of Eratosthenes Approach (0=not prime number)

    s[0].mark=0; //s[0].number=1 as on the function "initialize" I start from 1 not from 0
    int i,j;
    for (unsigned i = 2; i*i <size; i++)
    {
        if (s[i].mark == 1)
            for (unsigned j = i<<1;j<size;j+=i)
                s[j].mark = 0;
    }
}

int main(void)
{
    int i,j,prime_numbers[200];
    primal source_numbers[1000]; /* an array of source values */

    for(i=0;i<200;i++) prime_numbers[i]=0; /* initialize the prime numbers array to 0 */

    initialize(source_numbers,1000); /* initialize the source numbers array to hold the numbers 1-1000 */

    markPrimes(source_numbers,1000); /* identify the prime numbers in the source numbers array */

    /* copy the primes from the source numbers to the prime numbers array */
    for(i=0,j=0;i<1000;i++)
    {
        if(source_numbers[i].mark==1) /* if the current source number is a prime */
        {
            prime_numbers[j]=source_numbers[i].number; /* copy the number */
            j++; /* increment the target index */
        }
    }

    /* print the prime numbers */
    for(i=0,j=0;prime_numbers[i]!=0;i++,j++)
    {
        printf("%3d ",prime_numbers[i]);
        if(j==9) /* periodically print a newline and then reset j */
        {
            printf("\n");
            j=-1;
        }
    }
    return 0;
}

最佳答案

众所周知,C 允许程序员在他们的脚上打点(别担心,我们每个人都有这样的疤痕),这就是这里发生的事情。

在您的内部循环中,您从 1 开始,而不是 0,因此您需要将实际数字减一作为索引,并添加该数字而不是索引:

void markPrimes(primal *s,int size)
{
    //Brute Sieve of Eratosthenes Approach (0=not prime number)

    int i,j;
    // 1 (one) is not prime per definition
    s[0].mark = 0;
    for (i = 1; i*i <size; i++)
    {;
        if (s[i].mark == 1) {
            // you start at 1 instead of 0, so you need to take the actual number
            // minus one as the index and add that number instead of the index.
            for ( j = 2 * s[i].number  - 1;j < size; j += s[i].number){
                s[j].mark = 0;
            }
        }
    }
}

关于c - 埃拉托斯特尼筛法,素数 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43217138/

相关文章:

c - 希尔排序仅比冒泡排序快 3 倍?

javascript - 使用 JS RegEx 添加到字符串(作为数字)

javascript - 跨浏览器/引擎 Math.PI 始终为 3.141592653589793?

sql - 匹配多个字段组合的记录

java - 手动将 float 转换为二进制格式

c - C 中的中断处理

c - 得到一个太少的除数

检查 .txt 文件的格式在 C 中是否有效?

codeigniter - 如何使用 codeigniter 分页库回显页码?

java - 简单的 Java 数学运算