fork download
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. void kt(int l, int r)
  5. {
  6. bool prime[r - l + 1];
  7. memset(prime,true,sizeof(prime));
  8. for (long long i = 2; i * i <= r; ++i)
  9. {
  10. for (long long j = max(i * i, (l + (i - 1)) / i * i); j <= r; j += i)
  11. {
  12. prime[j - l] = false;
  13. }
  14. }
  15. long long j=0,csi,csj,maxx=0,minn=1e11;
  16. for (long long i = max(l, 2); i <= r; ++i)
  17. {
  18. if (prime[i - l])
  19. {
  20. if (j==0) j=i;
  21. else
  22. {
  23. if (i-j<minn)
  24. {
  25. minn=i-j;
  26. csi=i;
  27. csj=j;
  28. }
  29. j=i;
  30. }
  31. }
  32. }
  33. if (minn!=1e11)
  34. {
  35. cout<<csj<<","<<csi<<" are closest";
  36. }
  37. for (long long i = max(l, 2); i <= r; ++i)
  38. {
  39. if (prime[i - l])
  40. {
  41. if (j==0) j=i;
  42. else
  43. {
  44. if (i-j>maxx)
  45. {
  46. maxx=i-j;
  47. csi=i;
  48. csj=j;
  49. }
  50. j=i;
  51. }
  52. }
  53. }
  54. if (maxx!=0)
  55. {
  56. cout<<", ";
  57. cout<<csj<<","<<csi<<" are most distant." <<endl;
  58. return ;
  59. }
  60. cout<<"There are no adjacent primes."<<endl;
  61. }
  62. int main()
  63. {
  64. long long a,b,t;
  65. while(cin>>a>>b)
  66. {
  67. kt(a,b);
  68. }
  69. return 0;
  70. }
  71.  
Success #stdin #stdout 0s 4388KB
stdin
Standard input is empty
stdout
Standard output is empty