-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprimitive.cpp
111 lines (104 loc) · 3.19 KB
/
primitive.cpp
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
#include "primitive.h"
bool isPrimitiveRoot(uint64_t num, uint64_t of) {
if (gcd(num, of) != 1) {return false;}
uint64_t i = 1;
uint64_t ipow = 1;
while (i != phiFunction(of)) {
ipow *= num;
if (ipow % of == 1) {
return false;
}
ipow = ipow % of;
i++;
}
cout << "Calculating";
return true;
}
set<uint64_t> allPrimitiveRootsOf(uint64_t of) {
set<uint64_t> primitiveRoots;
for (uint64_t j = 1; j < of; j++) {
if (isPrimitiveRoot(j, of)) {
primitiveRoots.insert(j);
}
}
return primitiveRoots;
}
void writePrimitiveRoots(uint64_t of, string filename) {
ofstream out;
out.open(filename, fstream::app);
set<uint64_t> primitiveRoots = allPrimitiveRootsOf(of);
primitiveRoots = allPrimitiveRootsOf(of);
out << "Primitive roots of " << of << ":\n";
if (primitiveRoots.empty() || ((primitiveRoots.size() == 1) && (primitiveRoots.find(1) != primitiveRoots.end()))) {
out << "Has no primitive roots\n";
}
else {
for (set<uint64_t>::iterator it = primitiveRoots.begin(); it != primitiveRoots.end(); it++) {
out << *it << endl;
}
}
out << endl;
/*
time_t now = time(NULL);
tm * ptm = localtime(&now);
char buffer[32];
// Format: Mo, 15.06.2009 20:20:00
std::strftime(buffer, 32, "%a, %d.%m.%Y %H:%M:%S", ptm);
*/
out.close();
}
void printPrimitiveRoots(uint64_t of) {
set<uint64_t> primitiveRoots = allPrimitiveRootsOf(of);
primitiveRoots = allPrimitiveRootsOf(of);
cout << "Primitive roots of " << of << ":\n";
if (primitiveRoots.empty() || ((primitiveRoots.size() == 1) && (primitiveRoots.find(1) != primitiveRoots.end()))) {
cout << "Has no primitive roots\n";
}
else {
for (set<uint64_t>::iterator it = primitiveRoots.begin(); it != primitiveRoots.end(); it++) {
cout << *it << endl;
}
}
cout << endl;
}
void outPrimitiveRoots1_99 (string filename) {
ofstream out {filename};
//fstream out;
//out.open(filename, fstream::app);
set<uint64_t> primitiveRoots;
for (int of = 2; of < 100; of++) {
primitiveRoots = allPrimitiveRootsOf(of);
out << "Primitive roots of " << of << ":\n";
if (primitiveRoots.empty() || ((primitiveRoots.size() == 1) && (primitiveRoots.find(1) != primitiveRoots.end()))) {
out << "Has no primitive roots\n";
}
else {
for (set<uint64_t>::iterator it = primitiveRoots.begin(); it != primitiveRoots.end(); it++) {
out << *it << endl;
}
}
out << endl;
}
//out.close();
}
void clearPrimitive(string filename) {
ofstream out {filename};
out.clear();
}
set<int> primesto(int high) {
set<int> primes;
for (int i = 1; i < high + 1; i++) {
if (isPrime(i)) {
primes.insert(i);
}
}
return primes;
}
void outPrimes(string filename, int high) {
ofstream out {filename};
set<int> primes = primesto(high);
out << "Prime numbers to " << high << endl;
for (set<int>::iterator it = primes.begin(); it != primes.end(); it++) {
out << *it << endl;
}
}