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.