Calculates the largest and smallest prime factor for a set of numbers

0 0
  • 0 Collaborators

This project can factor the decomposed groups in the input array by parallel algorithm, and then compare the numbers with the largest minimum prime factor, and output the number and the factors after factoring from the smallest factor. ...learn more

Project status: Under Development

oneAPI, Artificial Intelligence

Intel Technologies
DevCloud, oneAPI

Overview / Usage

The parallel computing project I want to do can efficiently help users find the factorization result of each number in a set of data through the principle of parallel algorithms. The data is fed through changes in the main function, and finally outputs all the numbers that meet the condition and their factorization results. The full code is as follows (with comments):

#include

#include

#include

#include <ppl.h>

#include <concurrent_vector.h>

struct Factors

{

int number;

std::vector primes;

};

const int data[] =

{

12757923, 12878611, 12757923, 15808973, 15780709, 197622519 //这组是举例的数据,运行前可以任意输入,数据数目不限

};

int main()

{

// 并发安全容器取代了std::vector<>对象

Concurrency::concurrent_vector results;

// 并行算法取代了std::for_each()泛型算法

Concurrency::parallel_for_each(std::begin(data), std::end(data), [&](int n)

{

Factors factors;

factors.number = n;

for (int f = 2; n > 1; ++f)

{

while (n % f == 0)

{

factors.primes.push_back(f);

n /= f;

}

}

results.push_back(factors); // 对results对象因式分解

});

//并行计算结束

//查找results中的最大的最小素因数

auto max = std::max_element(results.begin(), results.end(), [](const Factors& a, const Factors& b)

{

return a.primes.front() < b.primes.front();

});

//输出满足条件的数(不一定唯一)及其各因数(由小到大)

std::for_each(results.begin(), results.end(), [&](const Factors& f)

{

if (f.primes.front() == max->primes.front())

{

std::cout << f.number << " = [ ";

std::copy(f.primes.begin(), f.primes.end(), std::ostream_iterator(std::cout, " "));

std::cout << "]\n";

}

});

return 0;

}

Methodology / Approach

C++ programming, parallel algorithms

Technologies Used

Visual Studio 2019

Comments (0)