Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- short int getN()
- {
- bool isIncorrect;
- short int ret =0;
- string inputLine;
- do
- {
- isIncorrect = false;
- cout << "Введите N"<< endl;
- getline(cin, inputLine);
- try
- {
- ret = stoi(inputLine);
- }
- catch (...)
- {
- cout << "N должно быть числом" <<endl;
- isIncorrect = true;
- }
- if ((ret < 1) || (ret > 64) && !isIncorrect)
- {
- cout << "N должно быть целым числом большим нуля и меньшим 65"<< endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- return ret;
- }
- void printArray(vector<long long int> arrayToPrint)
- {
- cout << "Числа мерсена равны" << endl;
- for (int i = 0; i < arrayToPrint.size(); i++)
- {
- cout << arrayToPrint[i] << endl;
- }
- }
- vector<int> getPrimes(int n)
- {
- vector<int> primes;
- for (int i = 0; i < n; i++)
- primes.push_back(i + 1);
- for (int i = 1; i < primes.size(); i++)
- if (primes[i] != 0)
- for (int j = i + 1; j < primes.size(); j++)
- if (primes[j] % primes[i] == 0)
- primes.erase(primes.begin()+j);
- return primes;
- }
- void GetMersennePrimes()
- {
- int n;
- n = getN();
- vector<int> primes = getPrimes(n);
- vector<long long int> mersennePrimes;
- for (int i = 0; i < primes.size(); i++)
- {
- mersennePrimes.push_back(pow(2, primes[i]) - 1);
- }
- printArray(mersennePrimes);
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- GetMersennePrimes();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement