Другие языки программирования и технологии

Помогите найти, алгоритм нахождения Произведения простых чисел, на С++, или литературу которая поможет разобраться.

Нужно найти произведение N-го и M-го простых чисел заданных с клавиатуры.
Сложнее, по-моему, задания ещё не было!
__
_Killer_ _Killer_
76 473
Лучший ответ
#include <iostream>
#include <sstream>
#include <string>

#include <vector>
#include <algorithm>

using namespace std;

bool IsPrime(int value, const vector<int> & primes) {
for (vector<int>::const_iterator i = primes.begin(); i != primes.end(); i++) {
const int prime = *i;
if (value / prime < prime) {
return true;
}

if (value % prime == 0) {
return false;
}
}

return true;
}

template <typename T>
T ReadValue(const string & message, const string & error_message) {
string line;
while (true) {
cout << message << ": ";

getline(cin, line);
istringstream stream(line);

T value;
stream >> value;

if (!stream || !stream.eof()) {
cout << error_message << endl;
}
else {
return value;
}
}
}

int ReadIndex(const string & message) {
while (true) {
const int value = ReadValue<int>(message, "not an integer");
if (value < 0) {
cout << "value must be positive" << endl;
}
else {
return value - 1; // base 1
}
}
}

int main() {
const int m = ReadIndex("m");
const int n = ReadIndex("n");
const int count = max(m, n) + 1;

vector<int> primes;
primes.reserve(count);

for (int value = 2; ; value++) {
if (IsPrime(value, primes)) {
primes.push_back(value);

if (primes.size() >= count) {
break;
}
}
}

const int result = primes[m] * primes[n];
cout << result << endl;
}

Похожие вопросы