Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int enterCheckedP() {
- const int MIN_NUM = 4;
- const int MAX_NUM = 45000;
- int P = 0;
- cin >> P;
- if ((P < MIN_NUM) || (P > MAX_NUM)) {
- cout << "Be careful, the number must be more than four and less than 45000! Try again:" << endl;
- enterCheckedP();
- }
- return P;
- }
- int *findPrimeNums(int P) {
- bool isPrime;
- int *tempPrimeNums = new int[P];
- int arrayCounter = 1;
- int counter = 0;
- tempPrimeNums[0] = 2;
- for (int i = 3; i <= P; i += 2) {
- isPrime = true;
- for (int j = 2; j < i; j++) {
- if (i % j == 0) {
- isPrime = false;
- }
- }
- if (isPrime) {
- arrayCounter++;
- tempPrimeNums[counter] = i;
- counter++;
- }
- }
- int *primeNums = new int[arrayCounter];
- for (int i = 0; i < arrayCounter; i++) {
- primeNums[i] = tempPrimeNums[i];
- }
- delete[] tempPrimeNums;
- return primeNums;
- }
- int *findProcessedNums(int* primeNums, int P) {
- int counter = 0;
- int *tempProcessedNums = new int[P];
- for (int i = 0; i < sizeof(primeNums); i++) {
- for (int j = i; j < sizeof(primeNums); j++) {
- if (primeNums[i] * primeNums[j] <= P) {
- tempProcessedNums[counter] = primeNums[i] * primeNums[j];
- counter++;
- }
- }
- }
- int *processedNums = new int[counter];
- for (int i = 0; i < counter; i++) {
- processedNums[i] = tempProcessedNums[i];
- }
- return processedNums;
- }
- int *sortByShuttle(int *processedNums) {
- for (int i = 1; i < sizeof(processedNums); i++) {
- if (processedNums[i] < processedNums[i - 1]) {
- int firstTempVal = processedNums[i];
- processedNums[i] = processedNums[i - 1];
- processedNums[i - 1] = firstTempVal;
- for (int j = i - 1; j > 0; j--) {
- if (processedNums[j] < processedNums[j - 1]) {
- int secondTempVal = processedNums[j];
- processedNums[j] = processedNums[j - 1];
- processedNums[j - 1] = secondTempVal;
- }
- }
- }
- }
- return processedNums;
- }
- void outputNums(int* processedNums) {
- processedNums = sortByShuttle(processedNums);
- cout << "Answer : ";
- for (int i = 0; i < sizeof(processedNums); i++) {
- cout << processedNums[i] << " ";
- }
- }
- void Main() {
- int P = enterCheckedP();
- int *primeNums = findPrimeNums(P);
- int *processedNums = findProcessedNums(primeNums, P);
- outputNums(processedNums);
- }
- int main()
- {
- Main();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement