Add program and README

master
Trivernis 4 years ago
parent 4b91830a0e
commit 1c4b0813fc

3
.gitignore vendored

@ -0,0 +1,3 @@
cmake-build-debug
primes.txt
main

2
.idea/.gitignore vendored

@ -0,0 +1,2 @@
# Default ignored files
/workspace.xml

@ -0,0 +1 @@
c___primes

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DiscordProjectSettings">
<option name="show" value="true" />
</component>
<component name="ProjectNotificationSettings">
<option name="askShowProject" value="false" />
</component>
</project>

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/c++-primes.iml" filepath="$PROJECT_DIR$/.idea/c++-primes.iml" />
</modules>
</component>
</project>

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.15)
project(c___primes)
set(CMAKE_CXX_STANDARD 20)
add_executable(c___primes main.cpp)

@ -1 +1,11 @@
c++-primes
# Primes in c++
## Usage
```bash
# build
g++ -lpthread -o main main.cpp
# run
./main
```

@ -0,0 +1,47 @@
#include <iostream>
#include <pthread.h>
#include <tuple>
#include <thread>
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);
}
Loading…
Cancel
Save