fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. //ham tinh can bac 2
  6. unsigned long my_sprt(unsigned long x)
  7. {
  8. unsigned long xn, xprev;
  9. xn = x/2;
  10. xprev = 0;
  11. while(xn !=xprev)
  12. {
  13. xprev = xn;
  14. xn = (xn + x/xprev)/2;
  15. }
  16. return xn;
  17. }
  18. //ham check xem so do co phai so nguyen to khong
  19. bool check_prime(int x)
  20. {
  21. if(x == 1 || x == 2)
  22. return true;
  23. for(int ii = 2; ii<x; ii++)
  24. {
  25. if((x % ii) == 0)
  26. return false;
  27. }
  28. return true;
  29. }
  30.  
  31. bool check_prime1(unsigned long x)
  32. {
  33. if(x == 1 || x == 2 || x == 3)
  34. return true;
  35. if(((x+1) % 6 == 0) || ((x - 1) % 6 == 0))
  36. {
  37. for(int ii = 2; ii <= my_sprt(x); ii++)
  38. {
  39. if((x % ii) == 0)
  40. return false;
  41. }
  42. return true;
  43. }
  44. return false;
  45. }
  46.  
  47. int main(int argc, char** argv)
  48. {
  49. int T, test_case;
  50. unsigned long n, m;
  51. // freopen("input.txt", "r", stdin);
  52.  
  53. cin >> T;
  54. for(test_case = 0; test_case < T; test_case++)
  55. {
  56. cin >> n >> m;
  57. for(int j = n; j<=m; j++)
  58. {
  59. if(check_prime1(j))
  60. cout << j << endl;
  61. }
  62. // Print the answer to standard output(screen).
  63. //cout << "Case #" << test_case+1 << endl;
  64. cout << endl;
  65. }
  66.  
  67. return 0;//Your program should return 0 on normal termination.
  68. }
  69.  
Runtime error #stdin #stdout 0.17s 2688KB
stdin
Standard input is empty
stdout