fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. long long l,r;
  6. while(cin>>l>>r)
  7. {
  8. long long near=3e9,far=-3e9;
  9. bool first=true,found=false;
  10. pair<long long,long long>pn,pf;
  11. long long before=-1,i=l;
  12. if(l>r)swap(l,r);
  13. if(l<=2)
  14. {
  15. before=2;
  16. first=false;
  17. i=3;
  18. }
  19. while(i<r)
  20. {
  21. if(i%2==1)
  22. {
  23. bool p=true;
  24. long long sq=sqrt(i);
  25. for(long long j=3;j<=sq;j+=2)
  26. {
  27. if(i%j==0)
  28. {
  29. p=false;
  30. break;
  31. }
  32.  
  33. }
  34. if(p)
  35. {
  36. if(first)
  37. {
  38. before=i;
  39. first=false;
  40. }
  41. else
  42. {
  43. found=true;
  44. if(near>i-before)
  45. {
  46. near=i-before;
  47. pn={before,i};
  48. }
  49. if(far<i-before)
  50. {
  51. far=i-before;
  52. pf={before,i};
  53. }
  54. before=i;
  55. }
  56. }
  57. }
  58. i+=1;
  59. }
  60. if(found)
  61. {
  62. printf("%d,%d are closest, %d,%d are most distant.\n",pn.first,pn.second,pf.first,pf.second);
  63. }
  64. else
  65. {
  66. printf("There are no adjacent primes.\n");
  67. }
  68. }
  69. }
Success #stdin #stdout 0.66s 4324KB
stdin
1 2
1 10
2 3
2 10
1 1000001
550 9214
500000 1500000
4 5
4 7
5 7
5 5
5 6
1500000 1500001
1500000 1500000
1500006 1500019
1500006 1500018
1500007 1500018
1500007 1500019
1500008 1500019
1500008 1500018
stdout
There are no adjacent primes.
2,3 are closest, 3,5 are most distant.
There are no adjacent primes.
2,3 are closest, 3,5 are most distant.
2,3 are closest, 492113,492227 are most distant.
569,571 are closest, 1327,1361 are most distant.
500111,500113 are closest, 1357201,1357333 are most distant.
There are no adjacent primes.
There are no adjacent primes.
There are no adjacent primes.
There are no adjacent primes.
There are no adjacent primes.
There are no adjacent primes.
There are no adjacent primes.
There are no adjacent primes.
There are no adjacent primes.
There are no adjacent primes.
There are no adjacent primes.
There are no adjacent primes.
There are no adjacent primes.