-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
123 lines (119 loc) · 3.92 KB
/
main.cpp
File metadata and controls
123 lines (119 loc) · 3.92 KB
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
/*
* Created by: Lugsole
* What: This is a prime finding function
* When: 2017 - Current
* Where:
* Why: This was made for fun
*/
#include <iostream>
#include <fstream>
#include <chrono>
#include <vector>
#include <climits>
#undef DEBUG
using namespace std; // Including the C++ Standard Library.
using namespace std::chrono; // Include chrono standard library
int main()
{
/* Log out an explanation of the max it can do */
cout << "Max can find " << UINT_MAX << endl;
cout << "Finding primes version v3" << endl
<< "Input the maximum number it will test." << endl;
unsigned int max_count;
/* Read the largest number it will be allowed to test against */
cin >> max_count;
vector<unsigned int> Prime_Numbers;
/* Start with two prime numbers */
Prime_Numbers.push_back(2);
Prime_Numbers.push_back(3);
/* Set the last number tested */
unsigned int spot = 4;
unsigned int Max_Try = 2;
unsigned int Max_Try_Squared = 4;
/* Get starting time to measure time taken */
high_resolution_clock::time_point t1 = high_resolution_clock::now();
/* Run through all */
while (spot <= max_count && spot > 0)
{
/* Declare number as prime (change latter is not true) */
bool is_prime = true;
/* Set the largest number to try the square root */
if(Max_Try_Squared <= spot){
++Max_Try;
Max_Try_Squared = Max_Try * Max_Try;
}
/* check against all the known prime numbers to see if it is prime or not */
for (unsigned int j = 0; j < Prime_Numbers.size() && Max_Try >= Prime_Numbers[j]; ++j)
{
/* if we are logging then print what index we are testing against */
#ifdef DEBUG
cout << "letter j:" << j << '=' << Prime_Numbers[j] << endl;
#endif
/* Get the number to test against */
unsigned int i = Prime_Numbers[j];
unsigned int n = spot;
/* see if not evenly divisible */
if (n % i == 0)
{
/* declare ads not prime and leave loop */
is_prime = false;
break;
}
}
/* check if the number is prime */
if (is_prime)
{
#ifdef DEBUG
/* log as prime */
cout << "Found prime: " << spot << endl;
#endif
/* add to list of prime */
Prime_Numbers.push_back(spot);
}
#ifdef DEBUG
/* if not prime */
else
{
/* Print Found not prime */
cout << "Found not prime: " << spot << endl;
}
#endif
/* increase spot checking */
spot++;
}
/* Create a second time to determine time taken */
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double, std::milli> time_span = t2 - t1;
/* Prints the time it took to the console */
cout << "Time used: " << time_span.count() << " ms"<< endl;
/* Crates a file stream */
ofstream myfile;
/* opens the file to write data to it */
myfile.open("example.txt");
/* Crates a file stream */
ofstream Uint_File;
/* opens the file to write data to it */
Uint_File.open("example.bin");
cout << "Writing prime numbers to files" << endl;
/* for all of the numbers print to file */
for (unsigned int j = 0; j < Prime_Numbers.size(); j++)
{
#ifdef DEBUG
cout << "prime:" << Prime_Numbers[j] << endl;
#endif
/* Prints to the file */
myfile << Prime_Numbers[j] << '\n';
Uint_File << (uint8_t)((Prime_Numbers[j])>>24);
Uint_File << (uint8_t)((Prime_Numbers[j])>>16);
Uint_File << (uint8_t)((Prime_Numbers[j])>>8);
Uint_File << (uint8_t)(Prime_Numbers[j]);
}
/* prints the number of prime numbers to the file */
myfile << "Found " << Prime_Numbers.size() << " primes" << endl;
/* prints amount of time consumed to the file */
myfile << "Total time consumed: " << time_span.count() << " ms" << endl;
/* close the file and save the file */
myfile.close();
Uint_File.close();
return 0;
}