작성
·
98
0
1,000번이나 100'000번까지 하면 바로 답이 나오는데 100만번으로 설정하면 계속 기다려도 감감무소식이라서 문제를 잘 모르겠습니다 ㅠㅠ.
#include "pch.h"
#include "CorePch.h"
#include "CoreMacro.h"
#include "ThreadManager.h"
#include <iostream>
// 소수 구하기
// 1과 자기 자신으로만 나뉘면 그것을 소수라고 함.
Mutex m;
bool CalculateRepeatToSelf(int32 selfNum) {
int8 count = 0;
for (int32 i = 2; i <= selfNum; i++) {
if (count > 2)
return false;
if (selfNum % i == 0) {
count++;
}
}
if (count == 1)
return true;
return false;
}
int32 PreCalcuate(int32 order, int32 devision, int32 interval) {
// 1, 10, 100'000
// 2, 10, 100'000
// 3, 10, 100'000
// ...
// 10, 10, 100'000
int32 startNum = (order - 1) * interval + 1;
int32 endNum = order * interval;
int32 count = 0;
for (int32 i = startNum; i <= endNum; i++) {
if (CalculateRepeatToSelf(i))
count++;
}
return count;
}
int main()
{
const int MAX_NUMBER = 1'000'000;
//const int MAX_NUMBER = 10'000; // 1229
//const int MAX_NUMBER = 1'000; // 168
// 1~MAX_NUMBER까지 소수 개수
// 멀티스레드로 병렬로 구해서 덧셈한다.
/*for (int32 i = 1; i <= 20; i++) {
if (CalculateRepeatToSelf(i))
count++;
}
cout << count << endl;*/
// ------------------------
vector<thread> threads;
int32 devision = thread::hardware_concurrency();
int32 interval = MAX_NUMBER / devision + 1;
atomic<int32> totalCount = 0;
for (int32 i = 1; i <= devision; i++) {
threads.push_back(thread([&totalCount, i, devision, interval](){
totalCount+= PreCalcuate(i, devision, interval);
}));
}
for (thread& t : threads) {
t.join();
}
cout << "Total: " << totalCount.load() << endl;
}
답변 1
0
#include <cmath>
#include <vector>
#include <atomic>
#include <thread>
bool IsPrime(int32 num) {
if (num <= 1) return false;
if (num <= 3) return true;
if (num % 2 == 0 || num % 3 == 0) return false;
for (int32 i = 5; i * i <= num; i += 6) {
if (num % i == 0 || num % (i + 2) == 0) return false;
}
return true;
}
int32 CountPrimesInRange(int32 start, int32 end) {
int32 count = 0;
for (int32 i = start; i <= end; i++) {
if (IsPrime(i)) count++;
}
return count;
}
int main() {
const int32 MAX_NUMBER = 1'000'000;
int32 threadCount = std::thread::hardware_concurrency();
std::vector<std::thread> threads;
std::atomic<int32> totalCount = 0;
int32 interval = MAX_NUMBER / threadCount;
for (int32 i = 0; i < threadCount; i++) {
int32 start = i * interval + 1;
int32 end = (i == threadCount - 1) ? MAX_NUMBER : (i + 1) * interval;
threads.emplace_back([start, end, &totalCount]() {
totalCount += CountPrimesInRange(start, end);
});
}
for (auto& t : threads) {
t.join();
}
std::cout << "Total primes up to " << MAX_NUMBER << ": " << totalCount << std::endl;
return 0;
}