Другие языки программирования и технологии
Помогите найти, алгоритм нахождения Произведения простых чисел, на С++, или литературу которая поможет разобраться.
Нужно найти произведение N-го и M-го простых чисел заданных с клавиатуры.
Сложнее, по-моему, задания ещё не было!
#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;
}
#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;
}
Похожие вопросы
- Помогите найти алгоритм подбора множителей к числам заданного массива, сумма произведений которых равна заданному числу
- Предложите алгоритм нахождения количества максимальных чисел из трех введенных чисел.
- Помогите найти алгоритм вычисления простых чисел
- Помогите пожалуйста! Как Разработать алгоритм нахождения суммы и кол-ва четных чисел натурального ряда, кот. >K, но
- Алгоритмы в паскале. Народ, напишите плиз алгоритм нахождения НОД и алгоритм выделения цифр числа. Заранее благодарю)
- Приведите алгоритмы нахождения простых чисел в заданном промежутке
- c++ сильно завис алгоритм нахождения простых чисел - пару вопросов ?
- Pascal . Дан массив вещественных чисел. Найти сумму элементов, номера которых являются простыми числами
- Помогите написать программу в Assembler НАЙТИ СУММУ ЦИФР 3-ЗНАЧНОГО ЧИСЛА И ПРОИЗВЕДЕНИЕ 4-ЗНАЧНОГО ЧИСЛА
- Помогите разобраться в C++ с программой по поиску простых чисел