-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprimefactors.cpp
More file actions
193 lines (154 loc) · 4.52 KB
/
primefactors.cpp
File metadata and controls
193 lines (154 loc) · 4.52 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* Copyright (C) 2019 Stefan Teleman.
*/
#include <iostream>
#include <iomanip>
#include <string>
#include <set>
#include <map>
#include <cmath>
#include <cstring>
#include <ctime>
#include <cerrno>
static std::multiset<uint64_t> Factors;
static std::map<uint64_t, uint32_t> FM;
static bool Check = false;
static struct timespec tp_start;
static struct timespec tp_end;
static int Timestamp(struct timespec* ts) {
if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ts) != 0) {
std::cerr << "clock_gettime(2) failed: " << strerror(errno)
<< std::endl;
return -1;
}
return 0;
}
static void PrintTimespec(const struct timespec* ts) {
std::cerr << "sec: " << std::setfill('0') << std::setw(9)
<< ts->tv_sec << std::endl;
std::cerr << "nns: " << std::setfill('0') << std::setw(12)
<< ts->tv_nsec << std::endl;
}
static void PrintTimediff(const struct timespec* start,
const struct timespec* end) {
uint64_t sec = (uint64_t) end->tv_sec - start->tv_sec;
uint64_t nns = (uint64_t) end->tv_nsec - start->tv_nsec;
if (nns >= 1000000000) {
sec += nns / 1000000000;
nns = nns % 1000000000;
}
std::cerr << "CPU time: " << sec << '.' << std::setfill('0') << std::setw(12)
<< nns << " second(s)." << std::endl;
}
static inline bool IsPrime(uint64_t N)
{
if (N == 0UL)
return false;
if (N <= 2UL)
return true;
if ((N & 1ULL) == 0)
return false;
uint64_t SQR = static_cast<uint64_t>(std::sqrt(N) + 1ULL);
for (uint64_t I = 3UL; I <= SQR; I += 2UL) {
if ((static_cast<uint64_t>(N % I)) == 0UL)
return false;
}
return true;
}
static void PrimeFactors(uint64_t N) {
uint64_t NX = N;
while ((NX % 2) == 0) {
Factors.insert(2);
NX /= 2;
}
uint64_t S = (uint64_t) std::sqrt(N);
for (uint64_t I = 3; I <= S; I += 2) {
while ((N % I) == 0) {
Factors.insert(I);
N /= I;
}
}
if (N > 2 && IsPrime(N))
Factors.insert(N);
if (NX > 2 && IsPrime(NX))
Factors.insert(NX);
}
static void CheckFactors() {
std::cout << "----------------------------" << std::endl;
for (std::map<uint64_t, uint32_t>::const_iterator I = FM.begin();
I != FM.end(); ++I) {
if (IsPrime((*I).first))
std::cout << (*I).first << " is prime." << std::endl;
else
std::cout << (*I).first << " is NOT prime." << std::endl;
}
std::cout << "----------------------------" << std::endl;
}
static void PrintUsage() {
std::cerr << "Usage: primefactors <unsigned-integer> [ --check ]" << std::endl;
}
static void PrintFactors(uint64_t N) {
std::cout << "Prime Factors of " << N << ":";
FM.clear();
for (std::multiset<uint64_t>::iterator I = Factors.begin();
I != Factors.end(); ++I) {
if (!(FM.insert(std::make_pair(*I, 1U)).second)) {
std::map<uint64_t, uint32_t>::iterator MI = FM.find(*I);
++((*MI).second);
}
}
for (std::map<uint64_t, uint32_t>::iterator I = FM.begin();
I != FM.end(); ++I) {
std::cout << " " << (*I).first;
}
std::cout << std::endl;
std::map<uint64_t, uint32_t>::const_iterator MIE;
std::cout << "Product: [";
for (std::map<uint64_t, uint32_t>::const_iterator MI = FM.begin();
MI != FM.end(); ++MI) {
if ((*MI).second == 1U)
std::cout << (*MI).first;
else
std::cout << '(' << (*MI).first << " ** " << (*MI).second << ')';
MIE = MI;
++MIE;
if (MIE != FM.end())
std::cout << " * ";
}
std::cout << ']' << std::endl;
}
int main(int argc, char* argv[])
{
if (argc < 2) {
PrintUsage();
return 1;
}
if (argc == 3 && strcmp(argv[2], "--check") == 0)
Check = true;
uint64_t N = (uint64_t) std::stoul(argv[1]);
Timestamp(&tp_start);
PrimeFactors(N);
Timestamp(&tp_end);
PrintFactors(N);
PrintTimediff(&tp_start, &tp_end);
if (Check) {
Timestamp(&tp_start);
CheckFactors();
Timestamp(&tp_end);
PrintTimediff(&tp_start, &tp_end);
}
return 0;
}