Jump to content

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>
{{libheader|GMP}}
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <vector>
 
int modpow(uint64_t base, uint64_t exp, uint64_t mod) {
#include <gmpxx.h>
if (mod == 1)
return 0;
uint64_t result = 1;
base %= mod;
for (; exp > 0; exp >>= 1) {
return (p + 1) %if (k(exp * n +& 1) == 0;1)
result = (result * base) % mod;
base = (base * base) % mod;
}
return result;
}
 
bool is_curzon(intuint64_t n, intuint64_t k) {
mpz_classuint64_t pm = k * n + 1;
return modpow(k, n, m) + 1 == m;
mpz_ui_pow_ui(p.get_mpz_t(), k, n);
return (p + 1) % (k * n + 1) == 0;
}
 
int main() {
for (intuint64_t k = 2; k <= 10; k += 2) {
std::cout << "Curzon numbers with base " << k << ":\n";
intuint64_t count = 0, n = 1;
for (; count < 50; ++n) {
if (is_curzon(n, k)) {
Line 1,088 ⟶ 1,098:
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">//fn [dependencies]modpow(mut base: usize, mut exp: usize, n: usize) -> usize {
// rug if n == "1.15.0" {
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
}
 
fn is_curzon(n: u32usize, k: u32usize) -> bool {
let m = k * n + 1;
modpow(k, n, m) + 1 == m
 
fn is_curzon(n: u32, k: u32) -> bool {
use rug::{Complete, Integer};
(Integer::u_pow_u(k, n).complete() + 1) % (k * n + 1) == 0
}
 
1,777

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.