-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
88 lines (67 loc) · 2.27 KB
/
Copy pathmain.cpp
File metadata and controls
88 lines (67 loc) · 2.27 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
#include <iostream> //cin, cout
#include <stdlib.h> //calloc, free
#include <fstream> //fstream
#include <ctime> //time()
void swap(unsigned long long *pA, unsigned long long *pB){ //swaps the values at two addresses
unsigned long long temp = *pA;
*pA = *pB;
*pB = temp;
return;
}
void export_primes(unsigned long long a, unsigned long long b, unsigned long long *count, std::string filename){
std::ofstream list(filename); //creates new file
unsigned long long tally = 0;
if(a > b){ //ensures that a is the smaller of the two numbers
swap(&a, &b);
}
if(b == 0){
//deal with a range of [0, 0]
*count = 0;
return;
}
/*
this is an implementation of the sieve of eratosthenes
positions in the array numbers[] will represent n, and the value at the position will indicate primality
prime numbers will be marked with 0 (false), and composite numbers will be marked with 1 (true)
*/
bool *numbers = (bool*) calloc(b + 1, sizeof(bool));
numbers[0] = true;
numbers[1] = true;
for(unsigned long long i = 2; i <= b; i++){
if(!numbers[i]){
if(i >= a){
tally++;
list << i << "\n";
}
for(unsigned long long j = i * 2; j <= b; j += i){
numbers[j] = true;
}
}
}
list << "total primes = " << tally;
*count = tally;
free(numbers);
list.close();
return;
}
int main(){
unsigned long long a, b, l, tally, t_start, t_end;
std::string filename;
std::cout << "---Prime Exporter---\nThis program uses the sieve of Eratosthenes to export a list of prime numbers to a file.\n\n";
std::cout << "Enter a: ";
std::cin >> a;
std::cout << "Enter b: ";
std::cin >> b;
std::cout << "Enter filename: ";
getline(std::cin >> std::ws, filename);
//this ensures that the new file ends in .txt
l = filename.length() - 4;
if(filename[l] != '.' || filename[l + 1] != 't' || filename[l + 2] != 'x' || filename[l + 3] != 't'){
filename += ".txt";
}
t_start = time(0);
export_primes(a, b, &tally, filename);
t_end = time(0);
std::cout << "\n" << tally << " primes exported in " << t_end - t_start << " seconds.\n";
return 0;
}