Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //very basic, just searches for mersenne primes. unoptimised. works
- //praštevila oblike 2^n -1
- #include <iostream>
- #include <fstream>
- #include <cmath> //pow(), sqrt()
- using namespace std;
- int main()
- {
- ofstream out_data("mersenne.txt");//naredi mersenne.txt
- do {
- unsigned long long int num = 0, max_n;
- bool prime = true;
- cout << "\n\nVpisi max stevilo: ";
- cin >> max_n;
- for (unsigned long long int power = 2; num <= max_n; power++) //vrti čez n; power na 2 na začetku saj 1 ni praštevilo.
- {
- num = pow(2, power) - 1; //dodeli vrednost 2^n - 1
- for (unsigned long long int i = 2; (i <= sqrt(num) +1) && (prime == true); i++) //testira če je praštevilo
- {
- if (num%i == 0)
- {
- prime = false;
- //cout << "\nFailed at pow" << power << "since div by " << i; (this debugs output and tells me everything is fucked up)
- }
- else
- prime = true;
- }
- if (prime == true)
- {
- cout << "\nMersenne prastevilo: 2^" << power<< " - 1 = " << num;
- out_data << "2^" << power << "-1 = " << num << endl; //zapiše rezultate v mersenne.txt
- }
- prime = true;
- }
- } while (true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement