Advertisement
tadejpetric

Untitled

Mar 25th, 2016
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 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");//naredi 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++) //vrti čez n; power na 2 na začetku saj 1 ni praštevilo.
  18.         {
  19.             num = pow(2, power) - 1; //dodeli vrednost 2^n - 1
  20.             for (unsigned long long int i = 2; (i <= sqrt(num) +1) && (prime == true); i++) //testira če je praštevilo
  21.             {
  22.                 if (num%i == 0)
  23.                 {
  24.                     prime = false;
  25.                     //cout << "\nFailed at pow" << power << "since div by " << i; (this debugs output and tells me everything is fucked up)
  26.                 }
  27.                 else
  28.                     prime = true;
  29.             }
  30.             if (prime == true)
  31.             {
  32.                 cout << "\nMersenne prastevilo: 2^" << power<< " - 1 = " << num;
  33.                 out_data << "2^" << power << "-1 = " << num << endl; //zapiše rezultate v mersenne.txt
  34.             }
  35.             prime = true;
  36.         }
  37.     } while (true);
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement