1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include <iostream>
#include <vector>
#include <algorithm> // to sort vector
#include <windows.h> // console handle
// as usual
using namespace std;
#define color_black 0
#define color_dark_blue 1
#define color_dark_green 2
#define color_light_blue 3
#define color_dark_red 4
#define color_magenta 5
#define color_orange 6
#define color_light_gray 7
#define color_gray 8
#define color_blue 9
#define color_green 10
#define color_cyan 11
#define color_red 12
#define color_pink 13
#define color_yellow 14
#define color_white 15
// functions' declaration
void intro();
void dataProcessing(int o);
void decToBinary(int n);
HANDLE hConsole;
vector<pair<int, int>> vgap; // pair for gap and its occurrence
int main()
{
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
int min, max, prime, o;
int previous = 0;
int gap = 0; // higher gap
int num = 0; // how many prime numbers have been found?
intro();
SetConsoleTextAttribute(hConsole, color_yellow);
cout << "Enter the lower limit -> ";
SetConsoleTextAttribute(hConsole, color_orange);
cin >> min;
SetConsoleTextAttribute(hConsole, color_yellow);
cout << "Enter the upper limit -> ";
SetConsoleTextAttribute(hConsole, color_orange);
cin >> max;
SetConsoleTextAttribute(hConsole, color_yellow);
cout << "Prime numbers list : " << endl << endl;
// check some bad entries
if (min >= max || min <= 1)
{
SetConsoleTextAttribute(hConsole, color_dark_red + (color_light_gray * 16));
cout << "*** Something goes wrong here! ***" << endl << endl;
SetConsoleTextAttribute(hConsole, color_white);
return 0;
}
for (int i = min; i <= max; i++)
{
prime = 1;
for (int j = 2; j < i; j++)
if (i % j == 0)
{
prime = 0;
break;
}
if (prime == 1)
{
if (previous == 0) previous = i;
num++;
SetConsoleTextAttribute(hConsole, color_cyan);
cout << i << "\t";
decToBinary(i);
o = (i - previous);
SetConsoleTextAttribute(hConsole, color_magenta);
if ((o != 0) ? cout << "\t" << o << endl : cout << "\t" << "*" << endl);
if (o > gap) gap = o; // new higher gap
dataProcessing(o);
previous = i;
}
}
// sort vector pair
sort(vgap.begin(), vgap.end());
// display results
SetConsoleTextAttribute(hConsole, color_yellow);
cout << "\n" << "--------------------------------------------" << endl;
cout << "Between numbers " << min << " and " << max << ", we got ";
SetConsoleTextAttribute(hConsole, color_orange);
cout << num;
SetConsoleTextAttribute(hConsole, color_yellow);
cout << " prime numbers (maximum gap : ";
SetConsoleTextAttribute(hConsole, color_orange);
cout << gap;
SetConsoleTextAttribute(hConsole, color_yellow);
cout << ") " << endl << endl;
for (int i = 1; i < vgap.size(); ++i)
{
SetConsoleTextAttribute(hConsole, color_cyan);
cout << "gap " << vgap[i].first;
SetConsoleTextAttribute(hConsole, color_orange);
cout << "\t\t" << vgap[i].second;
SetConsoleTextAttribute(hConsole, color_cyan);
cout << " occurrence(s)" << endl;
}
SetConsoleTextAttribute(hConsole, color_white);
return 0;
}
// simple intro function with info
void intro()
{
SetConsoleTextAttribute(hConsole, color_dark_red + (color_light_gray * 16));
cout << "**********************************" << endl;
cout << "*** Playing with prime numbers ***" << endl;
cout << "**********************************" << endl;
cout << endl;
SetConsoleTextAttribute(hConsole, color_white);
}
// data processing in vector pair
void dataProcessing(int o)
{
for (int i = 0; i < vgap.size(); i++)
if (vgap[i].first == o)
{ // so it exists - increment second ++
vgap[i].second++;
return;
}
// this point is only reached if the gap does not exist
vgap.push_back(make_pair(o, 1)); // create a new pair
}
// function to convert decimal to binary
void decToBinary(int n)
{
int binaryNum[1000] = {};
int bits = 8;
int i = 0;
while (n > 0)
{
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
if (i > 8)
bits = 8 * ((i + 7) / 8);
for (int j = bits - 1; j >= 0; j--)
{
if ((binaryNum[j] == 1) ? SetConsoleTextAttribute(hConsole, color_green) : SetConsoleTextAttribute(hConsole, color_dark_red));
cout << binaryNum[j];
}
}
|