Curzon numbers: Difference between revisions
C++ and Rust solutions updated to use modular exponentiation instead of arbitrary precision integers (as in the FreeBASIC solution).
No edit summary |
(C++ and Rust solutions updated to use modular exponentiation instead of arbitrary precision integers (as in the FreeBASIC solution).) |
||
Line 113:
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <cstdint>
#include <iostream>
#include <vector>
int modpow(uint64_t base, uint64_t exp, uint64_t mod) {
if (mod == 1)
return 0;
uint64_t result = 1;
base %= mod;
for (; exp > 0; exp >>= 1) {
result = (result * base) % mod;
base = (base * base) % mod;
}
return result;
}
bool is_curzon(
return modpow(k, n, m) + 1 == m;
▲ return (p + 1) % (k * n + 1) == 0;
}
int main() {
for (
std::cout << "Curzon numbers with base " << k << ":\n";
for (; count < 50; ++n) {
if (is_curzon(n, k)) {
Line 1,088 ⟶ 1,098:
=={{header|Rust}}==
<syntaxhighlight lang="rust">
return 0;
}
let mut result = 1;
base %= n;
while exp > 0 {
if (exp & 1) == 1 {
result = (result * base) % n;
}
base = (base * base) % n;
exp >>= 1;
}
result
}
let m = k * n + 1;
modpow(k, n, m) + 1 == m
▲fn is_curzon(n: u32, k: u32) -> bool {
}
|