U E D R , A S I H C RSS

소수구하기/zennith

No older revisions available

No older revisions available



~cpp 
#include <stdio.h>
#include <time.h>

#define MAX_PRIME 50000

int main(void) {
	int i, j, flag, arr_p, tmp, count = 0;

	int arr[10000] = {2, };

	time_t start, end;
	
	start = clock();
	
	arr_p = 1;

	for (i = 3; i < MAX_PRIME; i += 2) {
		for (j = 0, flag = 1, tmp = i >> 1; tmp >= arr[j]; j++) {
			if (i % arr[j] == 0) {
				flag = 0;
				break;
			}
		}

		if (flag) {
			arr[arr_p] = i;
			arr_p++;
		}
	}
	
	i = 0;

	end = clock();

	while (arr[i] != 0) {
		printf("%d ", arr[i++]);
		count++;
	}
	
	printf("\n%f\n", (double)(end - start) / CLK_TCK);
	printf("#%d prime numbers.\n", count);

	return 0;
}
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:30:13
Processing time 0.0168 sec