Advertisement
tuki2501

.cpp

Nov 12th, 2021
1,452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool isPrime(int n) {
  5.   if (n < 2) return 0;
  6.   for (int i = 2; i * i <= n; i++) {
  7.     if (n % i == 0) return false;
  8.   }
  9.   return true;
  10. }
  11.  
  12. bool isBinPal(int n) {
  13.   string s;
  14.   while (n) {
  15.     s += (n % 2) + '0';
  16.     n /= 2;
  17.   }
  18.   for (int i = 0; i < (int) s.size(); i++) {
  19.     if (s[i] != s[(int) s.size() - i - 1]) return false;
  20.   }
  21.   return true;
  22. }
  23.  
  24. int main() {
  25.   int cnt = 0;
  26.   for (int i = 1; i <= 9; i++)
  27.   for (int j = 0; j <= 9; j++) {
  28.     int n = i * 100 + j * 10 + i;
  29.     if (isBinPal(n)) {
  30.       cnt++;
  31.       cout << cnt << ' ' << n << ' ';
  32.       if (n <= (1 << 9)) cout << bitset<9>(n);
  33.       else cout << bitset<10>(n);
  34.       if (isPrime(n)) cout << " la so bi an can tim";
  35.       cout << '\n';
  36.     }
  37.   }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement