Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- using namespace std;
- bool isMersennePrime(int mersennePrime, int exponent)
- {
- bool isPrime = false;
- long long int s = 4;
- for (int i = 2; i < exponent; i++)
- {
- s = ((s * s) - 2) % mersennePrime;
- }
- if ((s == 0) || (mersennePrime == 3))
- {
- isPrime = true;
- }
- return isPrime;
- }
- unsigned int getN()
- {
- bool isIncorrect;
- unsigned int ret = 0;
- string inputLine;
- do
- {
- isIncorrect = false;
- cout << "Введите N" << endl;
- getline(cin, inputLine);
- try
- {
- ret = stoul(inputLine);
- }
- catch (...)
- {
- cout << "N должно быть положительным числом меньшим " << UINT32_MAX << endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- return ret;
- }
- void printArray(vector<int> arrayToPrint)
- {
- cout << "Числа мерсена равны" << endl;
- for (int i = 0; i < arrayToPrint.size(); i++)
- {
- cout << arrayToPrint[i] << endl;
- }
- }
- vector<int> getPrimes()
- {
- const int p = 32;
- vector<int> primes;
- for (int i = 0; i < p; 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()
- {
- unsigned int n;
- int mersennePrime;
- n = getN();
- vector<int> primes = getPrimes();
- vector<int> mersennePrimes;
- for (int i = 0; i < primes.size(); i++)
- {
- mersennePrime = (1 << primes[i]) - 1;
- if (isMersennePrime(mersennePrime, primes[i]) && n > mersennePrime)
- {
- mersennePrimes.push_back(mersennePrime);
- }
- }
- printArray(mersennePrimes);
- }
- int main()
- {
- setlocale(LC_ALL, "Russian");
- GetMersennePrimes();
- }
Add Comment
Please, Sign In to add comment