add trick example of greatest-common-divisor, knuth-morris-pratt, primality-test, quick-pow, split-text

This commit is contained in:
2025-09-23 11:25:26 +08:00
parent 57dd0e9440
commit 5368fcd277
5 changed files with 78 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
#include <vector>
bool primality_check(int n) {
std::vector<bool> is_prime(n + 1, true);
if (n <= 2) {
return false;
}
is_prime[0] = is_prime[1] = false;
for (int i = 2; i * i <= n; i++) {
if (is_prime[i]) {
for (int j = i * i; j <= n; j += i) {
is_prime[j] = false;
}
}
}
return is_prime[n];
}