mendoza_franco

TP01_EJERCICIO01

Sep 14th, 2021 (edited)
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4. unsigned t0, t1;
  5. /////////////////////////////////////////////////////////////////////////////////////////
  6.     bool esPrimo(int numero) {
  7.         if (numero == 0 || numero == 1 || numero == 4) return false;
  8.             for (int i = 2; i < numero / 2; i++) {
  9.                 if (numero % i == 0) return false;
  10.             }
  11.         //cout<<"\n\t > "<<numero<<" ES PRIMO"; //COMENTAR
  12.       return true;  }
  13. /////////////////////////////////////////////////////////////////////////////////////////
  14.     int main() {
  15.         int n,primos = 0;
  16.         bool numero = true;
  17.             cout<<"\n\t - INGRESE LA CANTIDAD DE NUMEROS (n):\t";
  18.                     cin>>n;
  19.             t0=clock();
  20.                         for(int i = 0; i <= n; i++){
  21.                             numero = esPrimo(i);
  22.                             if(numero == true)  primos ++;
  23.                         }
  24.                 cout<<"\n\n\t - CANTIDAD DE NUMEROS PRIMOS ENTRE (0 - "<<n<<"):\t"<<primos;
  25.             t1 = clock();
  26.         double tiempo = (double(t1-t0)/CLOCKS_PER_SEC);
  27.             cout<<"\n\t - TIEMPO DE EJECUCION:\t"<< tiempo << endl;
  28.     return 0;   }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment