我编写了一个用C语言计算质数的程序,作为一项任务,但它没有运行。

huangapple go评论57阅读模式
英文:

I wrote the program that calculate Prime numbers by C, as a assignment, but it didn't operate

问题

以下是我写的代码。

请告诉我为什么我的代码在执行之后很长时间后仍然无法正常工作。

我是在 num = 4 的条件下执行的。我使用了最新版本的Xcode和M2 MacBook Air。

#include <stdio.h>

int main() {
    int num = 0;
    printf("Enter the number of prime numbers you want to find : ");
    scanf("%d", &num);
    int prime[num], store = 0, load = 0, dividend = 1, signal = 1, i;
    prime[0] = 2;
    while (store < num) {
        for (load = 0; load < store; load++) {
            if (dividend % prime[load] == 0) {
                signal = signal * 0;
            }
        }
        if (signal == 1) { // if 'dividend' value is a prime number, store dividend in the array of 'prime'.
           prime[store] = dividend;
           store++;
        }
        signal = 1;
        dividend++;
    }
    for (i = 0; i < num; i++) {
        printf("%d \n", prime[i]);
    }
    return 0;
}

不久前,我向ChatGPT询问了这个问题。它告诉我:“这种方法已经过时了,你可以使用另一种方法,比如 埃拉托斯特尼筛法。”但至少,如果我的代码没有问题,它应该能够运行。

如果你需要进一步的帮助或解释,请告诉我。

英文:

Below is the code I wrote.

Please tell me why my code didn't work even after a long time after execution.

I did this under the condition of num = 4.
I used the latest version of Xcode, M2 MacBook Air.

#include &lt;stdio.h&gt;

int main() {
    int num = 0;
    printf(&quot;Enter the number of prime numbers you want to find : &quot;);
    scanf(&quot;%d&quot;, &amp;num);
    int prime[num], store = 0, load = 0, dividend = 1, signal = 1, i;
    prime[0] = 2;
    while (store &lt; num) {
        for (load = 0; load &lt; store; load++) {
            if (dividend % prime[load] == 0) {
                signal = signal * 0;
            }
        }
        if (signal == 1) { // if &#39;dividend&#39; value is a prime number, store dividend in the array of &#39;prime&#39;.
           prime[store] = dividend;
           store++;
        }
        signal = 1;
        dividend++;
    }
    for (i = 0; i &lt; num; i++) {
        printf(&quot;%d \n&quot;, prime[i]);
    }
    return 0;
}

/*
 signal == 1 : prime
 signal == 0 : not prime
 
 1. prime[load] == NALL
 2. for any value in the array &#39;prime&#39;, &#39;dividend % prime[load] != 0&#39; //dividend is prime number
    signal * 0 
*/

Jest a second ago, I asked chatGPT for this problem. It told me "This method is outdated. you can use another way like Sieve of Eratosthenes.".
But At least, if my code doesn't have problem, it must operate.

答案1

得分: 1

以下是翻译好的部分:

  1. store 应该被初始化为 1,因为你设置了初始值 prime[0] = 2 并以 dividend = 3 开始。你可以从 2 开始,然后删除 prime[0] 的初始化。
  2. signal 不是一个好的名字来表示素数指示器:composite 更明确。建议在发现因子后立即退出循环。
  3. loadcomposite 变量限定在循环内,并在循环开始时进行初始化,可以减少错误的发生。

以下是修改后的版本:

#include <stdio.h>

int main() {
    int num;

    printf("输入要找到的素数数量: ");
    if (scanf("%d", &num) != 1 || num < 1)
        return 1;

    int prime[num];
    int store = 0;
    int dividend = 2;

    while (store < num) {
        int composite = 0;
        for (int load = 0; load < store; load++) {
            if (dividend % prime[load] == 0) {
                composite = 1;
                break;
            }
        }
        if (composite == 0) {
            // 'dividend' 的值是一个素数,将其存储在数组中。
            prime[store] = dividend;
            store++;
        }
        dividend++;
    }
    for (int i = 0; i < num; i++) {
        printf("%d\n", prime[i]);
    }
    return 0;
}

请注意,测试所有素数因子并不是必需的。内循环的测试可以改为:

for (int load = 0; dividend / prime[load] >= prime[load]; load++) {
    if (dividend % prime[load] == 0) {
        composite = 1;
        break;
    }
}
英文:

There are multiple problems:

  • store should be initialized to 1 as you set the initial value prime[0] = 2 and start with dividend = 3. You could start at 2 and remove the initialization of prime[0].
  • signal is not a good name for the prime indicator: composite is more explicit. Breaking from the loop as soon as a factor has been found is also recommended.
  • making load and composite local to the loop and initialized at the beginning would be less error prone.

Here is a modified version:

#include &lt;stdio.h&gt;

int main() {
    int num;

    printf(&quot;Enter the number of prime numbers you want to find : &quot;);
    if (scanf(&quot;%d&quot;, &amp;num) != 1 || num &lt; 1)
        return 1;

    int prime[num];
    int store = 0;
    int dividend = 2;

    while (store &lt; num) {
        int composite = 0;
        for (int load = 0; load &lt; store; load++) {
            if (dividend % prime[load] == 0) {
                composite = 1;
                break;
            }
        }
        if (composite == 0) {
            // &#39;dividend&#39; value is a prime number, store it in the array.
            prime[store] = dividend;
            store++;
        }
        dividend++;
    }
    for (int i = 0; i &lt; num; i++) {
        printf(&quot;%d\n&quot;, prime[i]);
    }
    return 0;
}

Note that testing all prime factors is not necessary. The inner loop test could be changed to:

    for (int load = 0; dividend / prime[load] &gt;= prime[load]; load++) {
        if (dividend % prime[load] == 0) {
            composite = 1;
            break;
        }
    }

答案2

得分: 0

你应该声明 store = 1,因为质数数组中已经存储了一个数字。我认为导致无限循环的原因是,通过声明 dividend = 1 会使1进入质数数组,而每个数字都可以被1整除,导致算法无法检测到更多的质数。你应该声明 dividend = 3(跳过2,因为它已经在数组中)。

#include <stdio.h>
int main() {
    int num = 0;
    printf("Enter the number of prime numbers you want to find : ");
    scanf("%d", &num);
    int prime[num], store = 1, load = 0, dividend = 3, signal = 1, i;
    prime[0] = 2;
    while (store < num) {
        for (load = 0; load < store; load++) {
            if (dividend % prime[load] == 0) {
                signal = 0;
            }
        }
        if (signal == 1) { // 如果 'dividend' 的值是一个质数,将 dividend 存储在 'prime' 数组中。
           prime[store] = dividend;
           store++;
        }
        signal = 1;
        dividend++;
    }
    for (i = 0; i < num; i++) {
        printf("%d \n", prime[i]);
    }
    return 0;
}

还有一点要注意:
signal = signal * 0 等同于 signal = 0

英文:

You should declare store = 1 because there is a number stored in the prime array. I think the reason that is causing an infinite loop is that by declaring dividend = 1 will cause 1 to enter in the prime array, and every number is divisible by 1, causing the algorithm to detect no more primes. You should declare dividend = 3 (omiting 2 because is already in the array).

#include &lt;stdio.h&gt;
int main() {
    int num = 0;
    printf(&quot;Enter the number of prime numbers you want to find : &quot;);
    scanf(&quot;%d&quot;, &amp;num);
    int prime[num], store = 1, load = 0, dividend = 3, signal = 1, i;
    prime[0] = 2;
    while (store &lt; num) {
        for (load = 0; load &lt; store; load++) {
            if (dividend % prime[load] == 0) {
                signal = 0;
            }
        }
        if (signal == 1) { // if &#39;dividend&#39; value is a prime number, store dividend in the array of &#39;prime&#39;.
           prime[store] = dividend;
           store++;
        }
        signal = 1;
        dividend++;
    }
    for (i = 0; i &lt; num; i++) {
        printf(&quot;%d \n&quot;, prime[i]);
    }
    return 0;
}

One thing more:
signal = signal * 0 is equal to signal = 0

答案3

得分: 0

Your code has some more than one issue. For example, int prime[num];
you should create dynamic memory allocation or make it int prime[constant value]. Better to try to debug it 我编写了一个用C语言计算质数的程序,作为一项任务,但它没有运行。
Here is simple code about finding prime numbers example.

programiz.com/c-programming/examples/prime-number

英文:

Your code has some more than one issue. For example int prime[num] ;
you should create dynamic memory allocation or make it int prime[constant value]. Better to try debug it 我编写了一个用C语言计算质数的程序,作为一项任务,但它没有运行。
Here is simple code about finding prime numbers examle.

programiz.com/c-programming/examples/prime-number

huangapple
  • 本文由 发表于 2023年3月9日 19:25:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75683950.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定