daily pastebin goal
92%
SHARE
TWEET

Untitled

a guest Mar 13th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3.  
  4. bool isPrimal(int number);
  5. bool isDigitsSumPrimal(int number);
  6. bool isSuperPrimal(int number);
  7. bool isSuperBPrimal(int number);
  8.  
  9. std::string decToBin(int number);
  10.  
  11. int zadanieA(int begin, int end);
  12. int zadanieB1();
  13. bool zadanieB2();
  14.  
  15.  
  16. void main()
  17. {
  18.     std::cout << "<2, 1000>: " << zadanieA(2, 1000) << std::endl;
  19.     std::cout << "<100, 10000>: " << zadanieA(100, 10000) << std::endl;
  20.     std::cout << "<1000, 100000>: " << zadanieA(1000, 100000) << std::endl;
  21.  
  22.     std::cout << zadanieB1() << std::endl;
  23.     std::cout << zadanieB2() << std::endl;
  24.  
  25.     //for (int i = 0; i < 200; ++i)
  26.     //{
  27.     //  std::cout << i << ": " << isPrimal(i) << std::endl;
  28.     //}
  29.  
  30.     std::cin.get();
  31.  
  32. }
  33.  
  34.  
  35. bool isPrimal(int number)
  36. {
  37.     if (number <= 2) return false;
  38.  
  39.     for (int i = 2; i < number; ++i)
  40.     {
  41.         if (number % 2 == 0) return false;
  42.         if (number % i == 0) return false;
  43.     }
  44.     return true;
  45. }
  46.  
  47. bool isDigitsSumPrimal(int number)
  48. {
  49.     auto string_number = std::to_string(number);
  50.  
  51.     int sum = 0;
  52.     for (int i = 0; i < string_number.size(); ++i)
  53.     {
  54.         std::string one_digit = "";
  55.         one_digit += string_number[i];
  56.         sum += std::stoi(one_digit);
  57.     }
  58.  
  59.     //std::cout << number << ": " << string_number << " - " << sum << " : " << isPrimal(sum) << std::endl;
  60.  
  61.     return isPrimal(sum);
  62. }
  63.  
  64. bool isSuperPrimal(int number)
  65. {
  66.     return (isPrimal(number) && isDigitsSumPrimal(number));
  67. }
  68.  
  69. bool isSuperBPrimal(int number)
  70. {
  71.     if (!isPrimal(number)) return false;
  72.     std::string binary = decToBin(number);
  73.  
  74.     int sum = 0;
  75.  
  76.     for (int i = 0; i < binary.size(); ++i)
  77.     {
  78.         if (binary[i] == '1') {
  79.             ++sum;
  80.         }
  81.     }
  82.  
  83.     return isPrimal(sum);
  84. }
  85.  
  86. std::string decToBin(int number)
  87. {
  88.     std::string binary = "";
  89.     while (number > 0)
  90.     {
  91.         if (number % 2 == 0) binary += "0";
  92.         else binary += "1";
  93.         number /= 2;
  94.     }
  95.  
  96.     std::string reversed_binary = "";
  97.  
  98.     for (int i = binary.size() - 1; i >= 0; --i)
  99.     {
  100.         reversed_binary += binary[i];
  101.     }
  102.  
  103.     return reversed_binary;
  104. }
  105.  
  106. int zadanieA(int begin, int end)
  107. {
  108.     auto sum = 0;
  109.     for (int i = begin; i <= end; ++i)
  110.     {
  111.         if (isSuperPrimal(i) && isSuperBPrimal(i))
  112.         {
  113.             ++sum;
  114.         }
  115.     }
  116.  
  117.     return sum;
  118. }
  119.  
  120. int zadanieB1()
  121. {
  122.     auto sum = 0;
  123.     for (int i = 100; i <= 10000; ++i)
  124.     {
  125.         if (isDigitsSumPrimal(i)) ++sum;
  126.     }
  127.  
  128.     return sum;
  129. }
  130.  
  131. bool zadanieB2()
  132. {
  133.     auto sum = 0;
  134.     for (int i = 100; i < 10000; ++i)
  135.     {
  136.         if (isSuperBPrimal(i))
  137.         {
  138.             sum += i;
  139.         }
  140.     }
  141.  
  142.     return isPrimal(sum);
  143. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top