IT/C Language
소수의 합 구하기
Jang HyunWoong
2014. 12. 19. 09:40
알고리즘 중
---------------------------------------------------
10 이하의 소수를 모두 더하면 2+2+5+7 = 17이 됩니다.
이백만(2,000,000) 이하 소수의 합은 얼마입니까?
---------------------------------------------------
간단하게 C++ 로 구현해봤다.
- #include <iostream>
- using namespace std;
- int sosoo(int x);
- int main() {
- // your code goes here
- int result = 0;
- for(int i=1; i<=30; i++)
- {
- result += sosoo(i);
- }
- cout << "결과 = " << result << endl;
- return 0;
- }
- int sosoo(int x)
- {
- int count = 0;
- for(int j=1; j<=x; j++)
- {
- if(x%j == 0)
- {
- count++;
- }
- }
- if(count == 2)
- return x;
- else
- return 0;
- }
온라인으로 소스를 돌리기 때문에 숫자를 2백만까지 못했다.
30으로 테스트했다.
결과 = 129
반응형