Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. // primez.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. //#include "stdafx.h"
  5. #include <iostream>
  6. //#include <fstream>
  7. #include <cmath>
  8. #include <time.h>
  9. #include <stdio.h>
  10. using namespace std;
  11.  
  12. class Primez {
  13. public:
  14. int start;
  15. int finish;
  16. clock_t time;
  17. double timetaken;
  18. int counter;
  19. //ofstream myfile;
  20.  
  21. Primez(int start_arg, int finish_arg) {
  22. start = start_arg;
  23. finish = finish_arg;
  24. time = clock();
  25. counter = 0;
  26. //myfile.open("primez.csv");
  27.  
  28. for (int i = start; i <= finish; i++) {
  29. bool isaprime = isprime(i);
  30. if (isaprime == true) {
  31. //myfile << i;
  32. //myfile << "\n";
  33. printf("%d is a prime number\n",i);
  34. counter++;
  35. }
  36. }
  37. //myfile.close();
  38. time = clock() - time;
  39. timetaken = ((double)time) / CLOCKS_PER_SEC; //In seconds
  40. printf(" There is %d Prime numbers between %d and %d\nThe process took %f seconds\n", counter, start, finish, timetaken);
  41. }
  42.  
  43. bool isprime(int num) {
  44. bool isaprime = true;
  45. for (int i = 2; i < sqrt(num) + 1; i++) {
  46. if (num % i == 0) {
  47. isaprime = false;
  48. break;
  49. }
  50. else if (num == 0) {
  51. isaprime = false;
  52. break;
  53. }
  54. }
  55. return isaprime;
  56. }
  57. };
  58.  
  59. int main() {
  60. Primez a = Primez(1, 10000000);
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement