Last active
November 5, 2015 12:30
-
-
Save sjmulder/707f543fce0f56f10a47 to your computer and use it in GitHub Desktop.
Parallel primes in various languages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <future> | |
#include <cmath> | |
#include <cstdio> | |
using namespace std; | |
inline bool is_prime(int n) | |
{ | |
int upper = (int)floor(sqrt(n)); | |
for (int i = 2; i <= upper; i++) { | |
if (n % i == 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
vector<int> calc_primes(int min, int max, int num_threads) | |
{ | |
vector<int> primes; | |
if (num_threads > 1) { | |
int part_size = (int)ceil((float)(max - min + 1) / num_threads); | |
vector<future<vector<int>>> threads; | |
for (int i = 0; i < num_threads; i++) { | |
int t_min = min + i * part_size; | |
int t_max = std::min(t_min + part_size - 1, max); | |
threads.push_back(async(bind(calc_primes, t_min, t_max, 1))); | |
} | |
for (auto& t : threads) { | |
auto t_primes = t.get(); | |
primes.insert(primes.end(), t_primes.begin(), t_primes.end()); | |
} | |
} else { | |
for (int n = min; n <= max; n++) { | |
if (is_prime(n)) { | |
primes.push_back(n); | |
} | |
} | |
} | |
return primes; | |
} | |
int main() | |
{ | |
auto primes = calc_primes(20 * 1000 * 1000, 90 * 1000 * 1000, 4); | |
//auto primes = calc_primes(1000, 9000, 4); | |
cout << "Found " << primes.size() << " primes." << endl; | |
#if _DEBUG | |
cout << "Press any key to close." << endl; | |
getchar(); | |
#endif | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
using static System.Math; | |
using static System.Linq.Enumerable; | |
class Program | |
{ | |
// unsorted! | |
static IEnumerable<int> getPrimes(int min, int max, int numThreads) | |
{ | |
int partitionSize = (int)Ceiling((float)(max - min + 1) / numThreads); | |
return Range(0, numThreads) | |
// split workloads | |
.Select(i => Range(min, max - min + 1) | |
.Skip(partitionSize * i) | |
.Take(partitionSize)) | |
.AsParallel() | |
.SelectMany(ns => ns | |
// test prime | |
.Where(n => !Range(2, (int)Math.Sqrt(n) - 1) | |
.Any(i => n % i == 0))); | |
} | |
static void Main(string[] args) | |
{ | |
var primes = getPrimes(200 * 1000 * 1000, 900 * 1000 * 1000, 4); | |
Console.WriteLine($"Found {primes.Count()} primes"); | |
//foreach (int prime in primes) | |
// Console.WriteLine(prime); | |
Console.ReadKey(); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.sjmulder; | |
import java.util.ArrayList; | |
import java.util.stream.IntStream; | |
public class ParallelPrimes { | |
public static int[] getPrimes(int min, int max, int numThreads) { | |
int partitionSize = (int)Math.ceil((float)(max - min + 1) / numThreads); | |
ArrayList<IntStream> partitions = new ArrayList<>(); | |
IntStream.range(0, 4).forEach(i -> | |
partitions.add(IntStream | |
.range(min, max + 1) | |
.skip(partitionSize * i) | |
.limit(partitionSize))); | |
return partitions.parallelStream() | |
.flatMapToInt(r -> r.filter(n -> !IntStream | |
.range(2, (int)Math.floor(Math.sqrt(n)) + 1) | |
.anyMatch(i -> n % i == 0))) | |
.sorted() | |
.toArray(); | |
} | |
public static void main(String[] args) { | |
int[] primes = getPrimes(20 * 1000 * 1000, 90 * 1000 * 1000, 4); | |
//int[] primes = getPrimes(1000, 9000, 4); | |
System.out.println("Found " + primes.length + " primes"); | |
//for (int prime : primes) { | |
// System.out.println(prime); | |
//} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment