Advertisement
bolji_programer

Matematicki algoritmi - Prost broj

Jan 18th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. bool ProstBroj(int n)
  6. {
  7.     if (n==1) return false;
  8.     if (n==2) return true;
  9.  
  10.     int sqn=sqrt(n);
  11.  
  12.     for (int i=3;i<=sqn;i=i+2)
  13.         if (n%i==0) return false;
  14.  
  15.     return true;
  16. }
  17.  
  18. int main()
  19. {
  20.     int n;
  21.     cin>>n;
  22.  
  23.     if (ProstBroj(n)) cout<<"Uneseni broj je prost"<<endl;
  24.     else cout<<"Uneseni broj nije prost"<<endl;
  25.  
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement