본문 바로가기
Algorithm/Programmers

[Level1]소수의 합(에라토스테네스의 체) [연습문제] C/C++

by neohtux 2018. 10. 2.
728x90
#define TRUE 1
#define FALSE 0
#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    int answer = 0, Prime_count = 0;
    int isPrime = TRUE;

    int *arry;
    arry = (int*)malloc(sizeof(int)*(n*2));
    for (int i = 2; i < n; i++) // 1,0 거르고
    {
        arry[i] = i;
    }

    for (int i = 2; i <= n ;i++)
    {

if (arry[i]==FALSE) continue;

for (int k = i + i;k <= n; k = k + i) { /*k=4,6,8,10 ... 2의배수 k=6,9,12,15... 3의배수 k=8,12,16,20...4의배수 .......*/ arry[k] = FALSE; } if (arry[i]) Prime_count++; } answer = Prime_count; return answer; }


300x250

댓글