fork download
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. long long pri(long long x);
  9. int main()
  10. {
  11. /* Enter your code here. Read input from STDIN. Print output to STDOUT */
  12.  
  13. long long n,i,temp,temp1,sum=2;
  14. int isprime,count,count1,can;
  15. cin>>n;
  16.  
  17. if(n%2==0)
  18. n=n-1;
  19.  
  20.  
  21. while(n>2)
  22. {
  23. isprime=pri(n);
  24.  
  25. if(isprime==1)
  26. {
  27. //cout<<n<<endl;
  28. temp=0,temp1=0,count=0,count1=0,can=0,isprime=0;
  29. temp=n;
  30.  
  31.  
  32.  
  33. while(temp!=0)
  34. {
  35. temp=temp/10;
  36. count++;
  37. }
  38.  
  39. temp=n;
  40. count1=count;
  41. count1--;
  42. while(count!=1)
  43. {
  44. count--;
  45. temp1=temp%10;
  46. temp=temp/10;
  47. temp=temp+(temp1*pow(10,count1));
  48. //cout<<temp<<"swapped--"<<endl;
  49.  
  50. if(temp%2==0)
  51. {
  52. can=can+1;
  53. break;
  54. }
  55. else
  56. {
  57. isprime=pri(temp);
  58. //cout<<isprime<<"chk value"<<endl;
  59.  
  60. if(isprime==0)
  61. {
  62. can=can+1;
  63. //cout<<"can valeu"<<can<<endl;
  64. break;
  65. }
  66. }
  67.  
  68.  
  69. }
  70.  
  71. if(can==0)
  72. {
  73. sum=sum+n;
  74. //cout<<n<<"---Circular"<<endl;
  75. }
  76. }
  77.  
  78. n=n-2;
  79. }
  80.  
  81. cout<<sum<<endl;
  82. return 0;
  83. }
  84.  
  85.  
  86. long long pri(long long n)
  87. {
  88. int count=0;
  89. long long i,x;
  90. x=(n+1)/2;
  91. for(i=3;i<x;i=i+2)
  92. {
  93. if(n%i==0)
  94. {
  95. count++;
  96. return 0;
  97. }
  98.  
  99. }
  100.  
  101. if(count==0)
  102. return 1;
  103.  
  104.  
  105. return 0;
  106. }
  107.  
Time limit exceeded #stdin #stdout 5s 2684KB
stdin
Standard input is empty
stdout
Standard output is empty