英文:
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 <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;
}
/*
signal == 1 : prime
signal == 0 : not prime
1. prime[load] == NALL
2. for any value in the array 'prime', 'dividend % prime[load] != 0' //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
以下是翻译好的部分:
store
应该被初始化为1
,因为你设置了初始值prime[0] = 2
并以dividend = 3
开始。你可以从2
开始,然后删除prime[0]
的初始化。signal
不是一个好的名字来表示素数指示器:composite
更明确。建议在发现因子后立即退出循环。- 将
load
和composite
变量限定在循环内,并在循环开始时进行初始化,可以减少错误的发生。
以下是修改后的版本:
#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 to1
as you set the initial valueprime[0] = 2
and start withdividend = 3
. You could start at2
and remove the initialization ofprime[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
andcomposite
local to the loop and initialized at the beginning would be less error prone.
Here is a modified version:
#include <stdio.h>
int main() {
int num;
printf("Enter the number of prime numbers you want to find : ");
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' value is a prime number, store it in the array.
prime[store] = dividend;
store++;
}
dividend++;
}
for (int i = 0; i < num; i++) {
printf("%d\n", 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] >= 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 <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) { // 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;
}
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
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
Here is simple code about finding prime numbers examle.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论