-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
124 lines (101 loc) · 4.01 KB
/
main.cpp
File metadata and controls
124 lines (101 loc) · 4.01 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
#include <iostream>
#include <string>
#include <cmath>
#include <chrono>
bool isPrime(long long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
// Check divisors up to sqrt(n)
for (long long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
int main() {
std::string num; // Stored as a string to make truncating easier
std::cout << "Enter your number: ";
std::cin >> num;
// Time, for benchmarking
auto timeStart = std::chrono::system_clock::now();
auto millisStart = std::chrono::duration_cast<std::chrono::milliseconds>(
timeStart.time_since_epoch()
).count();
try {
long long initialCheck = std::stoll(num);
} catch (...) {
std::cerr << "Invalid input or number too large." << std::endl;
return -1;
}
if (isPrime(std::stoll(num))) {
std::cout << num << " is a prime number" << std::endl;
// Make sure it can be truncated
if (num.find('0') != std::string::npos) {
std::cout << "Truncatable primes cannot contain the digit 0." << std::endl;
std::cout << num << " is not a left truncatable prime number" << std::endl;
std::cout << num << " is not a right truncatable prime number" << std::endl;
std::cout << num << " is not a left-and-right truncatable prime number" << std::endl;
return 0;
}
// Check whether it's left truncatable
bool isLeftTruncatable = true;
for (int i = 0; i < num.length(); i++) {
std::string current = num.substr(i);
long long value = std::stoll(current);
if (!isPrime(value)) {
isLeftTruncatable = false;
break;
}
}
if (isLeftTruncatable)
std::cout << num << " is a left truncatable prime number" << std::endl;
else
std::cout << num << " is not a left truncatable prime number" << std::endl;
// Check whether it's right truncatable
bool isRightTruncatable = true;
for (int i = num.length(); i > 0; i--) {
std::string current = num.substr(0, i);
long long value = std::stoll(current);
if (!isPrime(value)) {
isRightTruncatable = false;
break;
}
}
if (isRightTruncatable)
std::cout << num << " is a right truncatable prime number" << std::endl;
else
std::cout << num << " is not a right truncatable prime number" << std::endl;
// Check whether it's left-and-right truncatable
bool isBothTruncatable = true;
std::string current = num;
if (current.length() > 3) {
while (!current.empty()) {
long long value = std::stoll(current);
if (!isPrime(value)) {
isBothTruncatable = false;
break;
}
// Remove one digit from both ends
if (current.length() <= 3) // If it has 2 or fewer digits, we’re done next
break;
current = current.substr(1, current.length() - 2);
}
} else {
isBothTruncatable = false;
}
if (isBothTruncatable)
std::cout << num << " is a left-and-right truncatable prime number" << std::endl;
else
std::cout << num << " is not a left-and-right truncatable prime number" << std::endl;
} else {
std::cout << num << " is not a prime number" << std::endl;
}
// Benchmark
auto timeEnd = std::chrono::system_clock::now();
auto millisEnd = std::chrono::duration_cast<std::chrono::milliseconds>(
timeEnd.time_since_epoch()
).count();
std::cout << "Evaluation completed in " << millisEnd - millisStart << " milliseconds" << std::endl;
return 0;
}