Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- void writeTask()
- {
- cout << "Данная программа находит все натуральные числа, которые можно представить в виде произведения двух простых чисел и которые не превосходят введённое натуральное число N\n";
- }
- short inputNumber()
- {
- const short MIN_NUMBER = 2;
- const short MAX_NUMBER = 127;
- bool isIncorrect;
- short number;
- do
- {
- isIncorrect = false;
- cout << "Введите натурально число N в диапазоне от " << MIN_NUMBER << " до " << MAX_NUMBER << endl;
- cin >> number;
- if (cin.fail())
- {
- cin.clear();
- while (cin.get() != '\n');
- cout << "Введены некорректные данные\n";
- isIncorrect = true;
- }
- if ((!isIncorrect) && (cin.get() != '\n'))
- {
- cin.clear();
- while (cin.get() != '\n');
- cout << "Введены некорректные данные\n";
- isIncorrect = true;
- }
- if ((!isIncorrect) && ((number > MAX_NUMBER) || (number < MIN_NUMBER)))
- {
- cout << "Неправильный диапазон данных\n";
- isIncorrect = true;
- }
- } while (isIncorrect);
- return number;
- }
- short* ConsistencyUpToNumber(short number)
- {
- short* arrayInt = new short[number - 1];
- int i = 0;
- while (i < number - 1)
- {
- arrayInt[i] = i + 2;
- i++;
- }
- return arrayInt;
- }
- short SecondDivision(short arrNumbersElement, int quotient)
- {
- const short arrOfPrimes[18] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61 };
- short arrCorrectnessElement = 0;
- int k = 0;
- while ((k < 18) && (arrOfPrimes[k] <= quotient))
- {
- if (quotient == arrOfPrimes[k])
- {
- arrCorrectnessElement = arrNumbersElement;
- k = 18;
- }
- else
- {
- arrCorrectnessElement = 0;
- k++;
- }
- }
- delete[] arrOfPrimes;
- return arrCorrectnessElement;
- }
- short* findingRequiredNumbers(short number)
- {
- const short arrOfPrimes[18] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61};
- short* arrNumbers = ConsistencyUpToNumber(number);
- number--;
- short* arrCorrectness = new short[number];
- for (int i = 0; i < number; i++)
- {
- arrCorrectness[i] = 0;
- int k = 0;
- while ((k < 18) && (arrOfPrimes[k] < arrNumbers[i]))
- {
- if (arrNumbers[i] % arrOfPrimes[k] == 0)
- {
- short quotient = arrNumbers[i] / arrOfPrimes[k];
- arrCorrectness[i] = SecondDivision(arrNumbers[i], quotient);
- k = 18;
- }
- k++;
- }
- }
- delete[] arrOfPrimes;
- delete[] arrNumbers;
- return arrCorrectness;
- }
- void outputNumber(short number, short* arrCorrect)
- {
- cout << "Полученные натуральные числа: \n";
- number--;
- short zeroValue = number;
- for (int i = 0; i < number; i++)
- {
- if (arrCorrect[i] != 0)
- {
- cout << arrCorrect[i] << " ";
- zeroValue++;
- }
- }
- if (zeroValue == number)
- {
- cout << "Натуральные числа, соответствующие требованиям, не найдены\n";
- }
- }
- int main()
- {
- setlocale(LC_ALL, "Rus");
- writeTask();
- short n = inputNumber();
- short* arrayOfCorrect = findingRequiredNumbers(n);
- outputNumber(n, arrayOfCorrect);
- delete[] arrayOfCorrect;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement