Advertisement
tadejpetric

Untitled

Mar 25th, 2016
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. //very basic, just searches for mersenne primes. unoptimised. works
  2. //praštevila oblike 2^n -1
  3. #include <iostream>
  4. #include <fstream>
  5. #include <cmath> //pow(), sqrt()
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     ofstream out_data("mersenne.txt");
  11.     do {
  12.         unsigned long long int num = 0, max_n;
  13.         bool prime = true;
  14.         cout << "\n\nVpisi max stevilo: ";
  15.         cin >> max_n;
  16.  
  17.         for (unsigned long long int power = 2; num <= max_n; power++) //loops powers
  18.         {
  19.             num = 1;
  20.             for (int i = 0; i < power; i++)
  21.                 num *= 2;
  22.             num--;
  23.             for (unsigned long long int i = 2; (i <= sqrt(num) +1) && (prime == true); i++) //tests if prime
  24.             {
  25.                 if (num%i == 0)
  26.                 {
  27.                     prime = false;
  28.                     cout << "\nFailed at pow " << power << " since div by " << i; //dej stran na konc, to za debugging
  29.                 }
  30.                 else
  31.                     prime = true;
  32.             }
  33.             if (prime == true)
  34.             {
  35.                 cout << "\nMersenne prastevilo: 2^" << power<< " - 1 = " << num;
  36.                 out_data << "2^" << power << "-1 = " << num << endl;
  37.             }
  38.             prime = true;
  39.         }
  40.     } while (true);
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement