Skip to content

Instantly share code, notes, and snippets.

@bwedding
Created December 5, 2019 01:37
Show Gist options
  • Save bwedding/5597a7b8912f010b258f6570e7e75ae0 to your computer and use it in GitHub Desktop.
Save bwedding/5597a7b8912f010b258f6570e7e75ae0 to your computer and use it in GitHub Desktop.
Parallel factorial
#include <iostream>
#include <algorithm>
#include <vector>
#include <execution>
#include <numeric>
long double Factorial(std::vector<int> & numbers)
{
long double result = 1.0;
std::for_each(
std::execution::par_unseq,
numbers.begin(),
numbers.end(),
[&](auto&& item)
{
result *= item;
});
return result;
}
int main()
{
long double result = 1.0;
std::vector<int> numbers(800);
std::iota(numbers.begin(), numbers.end(), 1);
std::cout << "800! is " << Factorial(numbers) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment