Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.59 KB | None | 0 0
  1. bool isPrime(int n) {
  2.  
  3. // Se e' pari non e' primo, fatta eccezione
  4.  
  5. // per il numero 2
  6.  
  7. if(n%2 == 0 && n!=2) return false;
  8.  
  9. // Controllo i primi scorrendo sino al massimo
  10.  
  11. // numero divisibile per n (ovvero n/2)
  12.  
  13. // Si puo' anche scorrere sino alla radice di n
  14.  
  15. for(int i=3; i<n/2; i+=2) {
  16.  
  17. if(n%i == 0) return false;
  18.  
  19. }
  20.  
  21.  
  22.  
  23. return true;
  24.  
  25. }
  26.  
  27. int main() {
  28.  
  29. int num, count=0;
  30.  
  31.  
  32.  
  33. cout << "Inserisci un numero: ";
  34.  
  35. cin >> num;
  36.  
  37.  
  38.  
  39. for(int i=1; i<num; i++) {
  40.  
  41. if(isPrime(i)) cout << i << endl;
  42.  
  43. }
  44.  
  45.  
  46.  
  47. return 0;
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement