Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.29 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int enterCheckedP() {
  6. const int MIN_NUM = 4;
  7. const int MAX_NUM = 45000;
  8. int P = 0;
  9. cin >> P;
  10. if ((P < MIN_NUM) || (P > MAX_NUM)) {
  11. cout << "Be careful, the number must be more than four and less than 45000! Try again:" << endl;
  12. enterCheckedP();
  13. }
  14. return P;
  15. }
  16.  
  17. int *findPrimeNums(int P) {
  18. bool isPrime;
  19. int *tempPrimeNums = new int[P];
  20. int arrayCounter = 1;
  21. int counter = 0;
  22. tempPrimeNums[0] = 2;
  23. for (int i = 3; i <= P; i += 2) {
  24. isPrime = true;
  25. for (int j = 2; j < i; j++) {
  26. if (i % j == 0) {
  27. isPrime = false;
  28. }
  29. }
  30. if (isPrime) {
  31. arrayCounter++;
  32. tempPrimeNums[counter] = i;
  33. counter++;
  34. }
  35. }
  36. int *primeNums = new int[arrayCounter];
  37. for (int i = 0; i < arrayCounter; i++) {
  38. primeNums[i] = tempPrimeNums[i];
  39. }
  40. delete[] tempPrimeNums;
  41. return primeNums;
  42. }
  43.  
  44. int *findProcessedNums(int* primeNums, int P) {
  45. int counter = 0;
  46. int *tempProcessedNums = new int[P];
  47. for (int i = 0; i < sizeof(primeNums); i++) {
  48. for (int j = i; j < sizeof(primeNums); j++) {
  49. if (primeNums[i] * primeNums[j] <= P) {
  50. tempProcessedNums[counter] = primeNums[i] * primeNums[j];
  51. counter++;
  52. }
  53. }
  54. }
  55. int *processedNums = new int[counter];
  56. for (int i = 0; i < counter; i++) {
  57. processedNums[i] = tempProcessedNums[i];
  58. }
  59. return processedNums;
  60. }
  61.  
  62. int *sortByShuttle(int *processedNums) {
  63. for (int i = 1; i < sizeof(processedNums); i++) {
  64. if (processedNums[i] < processedNums[i - 1]) {
  65. int firstTempVal = processedNums[i];
  66. processedNums[i] = processedNums[i - 1];
  67. processedNums[i - 1] = firstTempVal;
  68. for (int j = i - 1; j > 0; j--) {
  69. if (processedNums[j] < processedNums[j - 1]) {
  70. int secondTempVal = processedNums[j];
  71. processedNums[j] = processedNums[j - 1];
  72. processedNums[j - 1] = secondTempVal;
  73. }
  74. }
  75. }
  76. }
  77. return processedNums;
  78. }
  79.  
  80. void outputNums(int* processedNums) {
  81. processedNums = sortByShuttle(processedNums);
  82. cout << "Answer : ";
  83. for (int i = 0; i < sizeof(processedNums); i++) {
  84. cout << processedNums[i] << " ";
  85. }
  86. }
  87.  
  88. void Main() {
  89. int P = enterCheckedP();
  90. int *primeNums = findPrimeNums(P);
  91. int *processedNums = findProcessedNums(primeNums, P);
  92. outputNums(processedNums);
  93. }
  94.  
  95. int main()
  96. {
  97. Main();
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement