當我嘗試輸入大量數字(約1000萬)時,怎麼會收到「命令已終止」的消息?該程序顯示數字是否爲素數。輸入大數字時命令終止
見下面的代碼:
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
int main (int argc, char *argv[])
{
unsigned long long number;
bool prime (unsigned long long);
printf ("Enter a positive integer greater than 1: ");
scanf ("%llu", &number);
prime(number) ?
printf ("%llu is a prime.\n", number):
printf ("%llu is not a prime.\n", number);
return EXIT_SUCCESS;
}
bool prime (unsigned long long number)
{
unsigned long long i, j;
bool isPrime[number + 1];
for (i = 2; i <= number; i++)
isPrime[i] = true;
for (i = 2; i <= number - 1; i++)
for (j = 1; i*j <= number; j++)
isPrime[i*j] = false;
return isPrime[number];
}
行'bool isPrime [number + 1]'編譯沒有錯誤? –
@barakmanos它是C,而不是C++(C99支持可變長度數組) –
至少對我而言,是的。我正在使用GCC。 – vxs8122