diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ab155e2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +cmake-build-debug +primes.txt +main \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..c773b40 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +c___primes \ No newline at end of file diff --git a/.idea/c++-primes.iml b/.idea/c++-primes.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/c++-primes.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/.idea/discord.xml b/.idea/discord.xml new file mode 100644 index 0000000..59b11d1 --- /dev/null +++ b/.idea/discord.xml @@ -0,0 +1,9 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8822db8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..58390c1 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..f676b79 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.15) +project(c___primes) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(c___primes main.cpp) \ No newline at end of file diff --git a/README.md b/README.md index b3373c5..ece62e2 100644 --- a/README.md +++ b/README.md @@ -1 +1,11 @@ -c++-primes +# Primes in c++ + +## Usage + +```bash +# build +g++ -lpthread -o main main.cpp + +# run +./main +``` diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..02a5bcc --- /dev/null +++ b/main.cpp @@ -0,0 +1,47 @@ +#include +#include +#include +#include + +using namespace std; +using std::cout; + +uint numThreads; + +void *GetPrimes(void* start) { + ulong num = (ulong) start; + ulong incr = numThreads * 2; + while (true) { + bool isPrime = true; + if (num < 3 || num % 2 == 0) { + num += incr; + continue; + } + for (int i = 3; i < num/2; i += 2) { + if (num % i == 0) { + isPrime = false; + break; + } + } + if (isPrime) { + cout << num << "\n"; + } + num += incr; + } +} + +int main() { + numThreads = thread::hardware_concurrency(); + pthread_t threads[numThreads]; + long start = 1; + int rc; + for (int i = 0; i < numThreads; i++) { + rc = pthread_create(&threads[i], NULL, GetPrimes, (void *)(start + (2*i))); + if (rc) { + cout << "Error"; + exit(-1); + } + } + pthread_exit(NULL); +} +