From 1c4b0813fced1e6e49999e2ef49177b23d0661c5 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 27 Jan 2020 09:27:01 +0100 Subject: [PATCH] Add program and README --- .gitignore | 3 +++ .idea/.gitignore | 2 ++ .idea/.name | 1 + .idea/c++-primes.iml | 2 ++ .idea/discord.xml | 9 +++++++++ .idea/misc.xml | 7 +++++++ .idea/modules.xml | 8 ++++++++ .idea/vcs.xml | 6 ++++++ CMakeLists.txt | 6 ++++++ README.md | 12 ++++++++++- main.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 .idea/.gitignore create mode 100644 .idea/.name create mode 100644 .idea/c++-primes.iml create mode 100644 .idea/discord.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 CMakeLists.txt create mode 100644 main.cpp 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); +} +