Advertisement
pan7nikt

funkcja_logiczna_liczba_pierwsza

Feb 19th, 2020
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. bool pierwsza;
  6. int i;
  7. int lpowt;
  8. float input;
  9.  
  10. bool lpierwsza(float p)
  11. {
  12.    
  13.     pierwsza = true;
  14.     i = 2;
  15.     lpowt = 0;
  16.    
  17.     if (fmod(p, 2) == 0 && p != 2)
  18.     {
  19.         return false;
  20.     }
  21.     else
  22.     {
  23.         i = 3;
  24.     }
  25.     while(i<=sqrt(p) && pierwsza == true)
  26.     {
  27.         cout << i << endl;
  28.         cout << fmod(p,i) << endl;
  29.         cout << "=========" << endl;
  30.         lpowt++;
  31.        
  32.         if (fmod(p, i) == 0)
  33.         {
  34.            
  35.             pierwsza = false;
  36.             return false;
  37.         }
  38.         i= i+2;
  39.     }
  40.    
  41.     return true;
  42. }
  43.  
  44.  
  45. main()
  46. {
  47.     cout << "Podaj liczbe: ";
  48.     cin >> input;
  49.     cout << lpierwsza(input);
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement