Jump to content

Curzon numbers: Difference between revisions

m
m (→‎{{header|Raku}}: Format output in groups of 10 rather than 25 to reduce line wrapping)
 
(13 intermediate revisions by 9 users not shown)
Line 255:
 
1000th Curzon with base 10 = 46845</pre>
 
=={{header|C}}==
{{trans|C++}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <locale.h>
 
uint64_t 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) {
if ((exp & 1) == 1) result = (result * base) % mod;
base = (base * base) % mod;
}
return result;
}
 
bool isCurzon(uint64_t n, uint64_t k) {
const uint64_t r = k * n;
return modPow(k, n, r+1) == r;
}
 
int main() {
uint64_t k, n, count;
setlocale(LC_NUMERIC, "");
for (k = 2; k <= 10; k += 2) {
printf("Curzon numbers with base %ld:\n", k);
for (n = 1, count = 0; count < 50; ++n) {
if (isCurzon(n, k)) {
printf("%4ld ", n);
if (++count % 10 == 0) printf("\n");
}
}
for (;;) {
if (isCurzon(n, k)) ++count;
if (count == 1000) break;
++n;
}
printf("1,000th Curzon number with base %ld: %'ld\n\n", k, n);
}
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Curzon numbers with base 2:
1 2 5 6 9 14 18 21 26 29
30 33 41 50 53 54 65 69 74 78
81 86 89 90 98 105 113 114 125 134
138 141 146 153 158 165 173 174 186 189
194 198 209 210 221 230 233 245 249 254
1,000th Curzon number with base 2: 8,646
 
Curzon numbers with base 4:
1 3 7 9 13 15 25 27 37 39
43 45 49 57 67 69 73 79 87 93
97 99 105 115 127 135 139 153 163 165
169 175 177 183 189 193 199 205 207 213
219 235 249 253 255 265 267 273 277 279
1,000th Curzon number with base 4: 9,375
 
Curzon numbers with base 6:
1 6 30 58 70 73 90 101 105 121
125 146 153 166 170 181 182 185 210 233
241 242 266 282 290 322 373 381 385 390
397 441 445 446 450 453 530 557 562 585
593 601 602 605 606 621 646 653 670 685
1,000th Curzon number with base 6: 20,717
 
Curzon numbers with base 8:
1 14 35 44 72 74 77 129 131 137
144 149 150 185 200 219 236 266 284 285
299 309 336 357 381 386 390 392 402 414
420 441 455 459 470 479 500 519 527 536
557 582 600 602 617 639 654 674 696 735
1,000th Curzon number with base 8: 22,176
 
Curzon numbers with base 10:
1 9 10 25 106 145 190 193 238 253
306 318 349 385 402 462 486 526 610 649
658 678 733 762 810 990 994 1033 1077 1125
1126 1141 1149 1230 1405 1422 1441 1485 1509 1510
1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
1,000th Curzon number with base 10: 46,845
</pre>
 
=={{header|C++}}==
Line 346 ⟶ 433:
 
</pre>
 
=={{header|EasyLang}}==
{{trans|FreeBASIC}}
<syntaxhighlight>
func pow_mod b power modulus .
x = 1
while power > 0
if power mod 2 = 1
x = x * b mod modulus
.
b = b * b mod modulus
power = power div 2
.
return x
.
for k = 2 step 2 to 10
numfmt 0 0
print "First 50 Curzon numbers using a base of " & k & ":"
numfmt 0 4
n = 1
count = 0
repeat
m = k * n + 1
p = pow_mod k n m + 1
if p = m
count += 1
if count <= 50
write " " & n
if count mod 9 = 0
print ""
.
.
.
until count = 1000
n += 1
.
print "" ; print "One thousandth: " & n
print ""
.
</syntaxhighlight>
 
=={{header|Factor}}==
Line 529 ⟶ 656:
=={{header|Go}}==
{{trans|Wren}}
Based on Version 1.
<syntaxhighlight lang="go">package main
 
Line 763 ⟶ 891:
| |1513 1606 1614 1630 1665 1681 1690 1702 1785 1837| |
+----+-------------------------------------------------+------+
</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
 
public final class CurzonNumbers {
 
public static void main(String[] aArgs) {
for ( int k = 2; k <= 10; k += 2 ) {
System.out.println("Generalised Curzon numbers with base " + k + ":");
int n = 1;
int count = 0;
while ( count < 50 ) {
if ( isGeneralisedCurzonNumber(k, n) ) {
System.out.print(String.format("%4d%s", n, ( ++count % 10 == 0 ? "\n" : " " )));
}
n += 1;
}
while ( count < 1_000 ) {
if ( isGeneralisedCurzonNumber(k, n) ) {
count += 1;
}
n += 1;
}
System.out.println("1,000th Generalised Curzon number with base " + k + ": " + ( n - 1 ));
System.out.println();
}
}
private static boolean isGeneralisedCurzonNumber(int aK, int aN) {
final long r = aK * aN;
return modulusPower(aK, aN, r + 1) == r;
}
private static long modulusPower(long aBase, long aExponent, long aModulus) {
if ( aModulus == 1 ) {
return 0;
}
aBase %= aModulus;
long result = 1;
while ( aExponent > 0 ) {
if ( ( aExponent & 1 ) == 1 ) {
result = ( result * aBase ) % aModulus;
}
aBase = ( aBase * aBase ) % aModulus;
aExponent >>= 1;
}
return result;
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
Generalised Curzon numbers with base 2:
1 2 5 6 9 14 18 21 26 29
30 33 41 50 53 54 65 69 74 78
81 86 89 90 98 105 113 114 125 134
138 141 146 153 158 165 173 174 186 189
194 198 209 210 221 230 233 245 249 254
1,000th Generalised Curzon number with base 2: 8646
 
Generalised Curzon numbers with base 4:
1 3 7 9 13 15 25 27 37 39
43 45 49 57 67 69 73 79 87 93
97 99 105 115 127 135 139 153 163 165
169 175 177 183 189 193 199 205 207 213
219 235 249 253 255 265 267 273 277 279
1,000th Generalised Curzon number with base 4: 9375
 
Generalised Curzon numbers with base 6:
1 6 30 58 70 73 90 101 105 121
125 146 153 166 170 181 182 185 210 233
241 242 266 282 290 322 373 381 385 390
397 441 445 446 450 453 530 557 562 585
593 601 602 605 606 621 646 653 670 685
1,000th Generalised Curzon number with base 6: 20717
 
Generalised Curzon numbers with base 8:
1 14 35 44 72 74 77 129 131 137
144 149 150 185 200 219 236 266 284 285
299 309 336 357 381 386 390 392 402 414
420 441 455 459 470 479 500 519 527 536
557 582 600 602 617 639 654 674 696 735
1,000th Generalised Curzon number with base 8: 22176
 
Generalised Curzon numbers with base 10:
1 9 10 25 106 145 190 193 238 253
306 318 349 385 402 462 486 526 610 649
658 678 733 762 810 990 994 1033 1077 1125
1126 1141 1149 1230 1405 1422 1441 1485 1509 1510
1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
1,000th Generalised Curzon number with base 10: 46845
</pre>
 
Line 920 ⟶ 1,143:
 
</syntaxhighlight>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Predicate function that checks wether a positive integer is generalized k-Curzon number or not */
g_curzonp(n,k):=if mod((k^n)+1,k*n+1)=0 then true$
 
/* Function that returns a list of the first len generalized k-Curzon numbers */
g_curzon_count(len,k):=block(
[i:1,count:0,result:[]],
while count<len do (if g_curzonp(i,k) then (result:endcons(i,result),count:count+1),i:i+1),
result)$
 
/* Test cases */
g_curzon_count(50,2);
g_curzon_count(50,4);
g_curzon_count(50,6);
g_curzon_count(50,8);
g_curzon_count(50,10);
</syntaxhighlight>
{{out}}
<pre>
[1,2,5,6,9,14,18,21,26,29,30,33,41,50,53,54,65,69,74,78,81,86,89,90,98,105,113,114,125,134,138,141,146,153,158,165,173,174,186,189,194,198,209,210,221,230,233,245,249,254]
[1,3,7,9,13,15,25,27,37,39,43,45,49,57,67,69,73,79,87,93,97,99,105,115,127,135,139,153,163,165,169,175,177,183,189,193,199,205,207,213,219,235,249,253,255,265,267,273,277,279]
[1,6,30,58,70,73,90,101,105,121,125,146,153,166,170,181,182,185,210,233,241,242,266,282,290,322,373,381,385,390,397,441,445,446,450,453,530,557,562,585,593,601,602,605,606,621,646,653,670,685]
[1,14,35,44,72,74,77,129,131,137,144,149,150,185,200,219,236,266,284,285,299,309,336,357,381,386,390,392,402,414,420,441,455,459,470,479,500,519,527,536,557,582,600,602,617,639,654,674,696,735]
[1,9,10,25,106,145,190,193,238,253,306,318,349,385,402,462,486,526,610,649,658,678,733,762,810,990,994,1033,1077,1125,1126,1141,1149,1230,1405,1422,1441,1485,1509,1510,1513,1606,1614,1630,1665,1681,1690,1702,1785,1837]
</pre>
 
=={{header|Nim}}==
Nim standard library doesn’t provide a modular power, so we have to define our own function.
<syntaxhighlight lang="Nim">import std/strformat
 
func pow(a, n: Natural; m: Positive): Natural =
var a = a mod m
var n = n
if a > 0:
result = 1
while n > 0:
if (n and 1) != 0:
result = (result * a) mod m
n = n shr 1
a = (a * a) mod m
 
iterator curzonNumbers(k: Positive): Natural =
assert (k and 1) == 0, "base must be even."
var n = 1
while true:
let m = k * n + 1
if pow(k, n, m) + 1 == m:
yield n
inc n
 
### Task ###
for k in countup(2, 10, 2):
echo &"Curzon numbers for k = {k}:"
var count = 0
for n in curzonNumbers(k):
inc count
stdout.write &"{n:>4}"
stdout.write if count mod 10 == 0: '\n' else: ' '
if count == 50: break
echo()
 
### Stretch task ###
for k in countup(2, 10, 2):
var count = 0
for n in curzonNumbers(k):
inc count
if count == 1000:
echo &"1000th Curzon number for k = {k:>2}: {n:>5}"
break
</syntaxhighlight>
{{out}}
<pre>Curzon numbers for k = 2:
1 2 5 6 9 14 18 21 26 29
30 33 41 50 53 54 65 69 74 78
81 86 89 90 98 105 113 114 125 134
138 141 146 153 158 165 173 174 186 189
194 198 209 210 221 230 233 245 249 254
 
Curzon numbers for k = 4:
1 3 7 9 13 15 25 27 37 39
43 45 49 57 67 69 73 79 87 93
97 99 105 115 127 135 139 153 163 165
169 175 177 183 189 193 199 205 207 213
219 235 249 253 255 265 267 273 277 279
 
Curzon numbers for k = 6:
1 6 30 58 70 73 90 101 105 121
125 146 153 166 170 181 182 185 210 233
241 242 266 282 290 322 373 381 385 390
397 441 445 446 450 453 530 557 562 585
593 601 602 605 606 621 646 653 670 685
 
Curzon numbers for k = 8:
1 14 35 44 72 74 77 129 131 137
144 149 150 185 200 219 236 266 284 285
299 309 336 357 381 386 390 392 402 414
420 441 455 459 470 479 500 519 527 536
557 582 600 602 617 639 654 674 696 735
 
Curzon numbers for k = 10:
1 9 10 25 106 145 190 193 238 253
306 318 349 385 402 462 486 526 610 649
658 678 733 762 810 990 994 1033 1077 1125
1126 1141 1149 1230 1405 1422 1441 1485 1509 1510
1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
 
1000th Curzon number for k = 2: 8646
1000th Curzon number for k = 4: 9375
1000th Curzon number for k = 6: 20717
1000th Curzon number for k = 8: 22176
1000th Curzon number for k = 10: 46845
</pre>
 
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let modpow m =
let rec loop p b e =
if e land 1 = 0
then if e = 0 then p else loop p (b * b mod m) (e lsr 1)
else loop (p * b mod m) (b * b mod m) (e lsr 1)
in loop 1
 
let is_curzon k n =
let r = k * n in r = modpow (succ r) k n
 
let () =
List.iter (fun x ->
Seq.(ints 0 |> filter (is_curzon x) |> take 50 |> map string_of_int)
|> List.of_seq |> String.concat " " |> Printf.printf "base %u:\n%s\n" x)
[2; 4; 6; 8; 10]
 
let () =
List.iter (fun x ->
Seq.(ints 0 |> filter (is_curzon x) |> drop 999 |> take 1
|> iter (Printf.printf "base %u (1000th): %u\n" x)))
[2; 4; 6; 8; 10]</syntaxhighlight>
<pre>
base 2:
1 2 5 6 9 14 18 21 26 29 30 33 41 50 53 54 65 69 74 78 81 86 89 90 98 105 113 114 125 134 138 141 146 153 158 165 173 174 186 189 194 198 209 210 221 230 233 245 249 254
base 4:
1 3 7 9 13 15 25 27 37 39 43 45 49 57 67 69 73 79 87 93 97 99 105 115 127 135 139 153 163 165 169 175 177 183 189 193 199 205 207 213 219 235 249 253 255 265 267 273 277 279
base 6:
1 6 30 58 70 73 90 101 105 121 125 146 153 166 170 181 182 185 210 233 241 242 266 282 290 322 373 381 385 390 397 441 445 446 450 453 530 557 562 585 593 601 602 605 606 621 646 653 670 685
base 8:
1 14 35 44 72 74 77 129 131 137 144 149 150 185 200 219 236 266 284 285 299 309 336 357 381 386 390 392 402 414 420 441 455 459 470 479 500 519 527 536 557 582 600 602 617 639 654 674 696 735
base 10:
1 9 10 25 106 145 190 193 238 253 306 318 349 385 402 462 486 526 610 649 658 678 733 762 810 990 994 1033 1077 1125 1126 1141 1149 1230 1405 1422 1441 1485 1509 1510 1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
base 2 (1000th): 8646
base 4 (1000th): 9375
base 6 (1000th): 20717
base 8 (1000th): 22176
base 10 (1000th): 46845
</pre>
 
=={{header|Odin}}==
<syntaxhighlight lang="Go">
package curzon_numbers
/* imports */
import "core:c/libc"
import "core:fmt"
/* main block */
main :: proc() {
for k: int = 2; k <= 10; k += 2 {
fmt.println("\nCurzon numbers with base ", k)
count := 0
n: int = 1
for ; count < 50; n += 1 {
if is_curzon(n, k) {
count += 1
libc.printf("%*d ", 4, n)
if (count) % 10 == 0 {
fmt.printf("\n")
}
}
}
for {
if is_curzon(n, k) {
count += 1}
if count == 1000 {
break}
n += 1
}
libc.printf("1000th Curzon number with base %d: %d \n", k, n)
}
}
/* definitions */
modpow :: proc(base, exp, mod: int) -> int {
if mod == 1 {
return 0}
result: int = 1
base := base
exp := exp
base %= mod
for ; exp > 0; exp >>= 1 {
if ((exp & 1) == 1) {
result = (result * base) % mod}
base = (base * base) % mod
}
return result
}
 
is_curzon :: proc(n: int, k: int) -> bool {
r := k * n //const?
return modpow(k, n, r + 1) == r
}
</syntaxhighlight>
{{out}}
<pre>
Curzon numbers with base 2
1 2 5 6 9 14 18 21 26 29
30 33 41 50 53 54 65 69 74 78
81 86 89 90 98 105 113 114 125 134
138 141 146 153 158 165 173 174 186 189
194 198 209 210 221 230 233 245 249 254
1000th Curzon number with base 2: 8646
 
Curzon numbers with base 4
1 3 7 9 13 15 25 27 37 39
43 45 49 57 67 69 73 79 87 93
97 99 105 115 127 135 139 153 163 165
169 175 177 183 189 193 199 205 207 213
219 235 249 253 255 265 267 273 277 279
1000th Curzon number with base 4: 9375
 
Curzon numbers with base 6
1 6 30 58 70 73 90 101 105 121
125 146 153 166 170 181 182 185 210 233
241 242 266 282 290 322 373 381 385 390
397 441 445 446 450 453 530 557 562 585
593 601 602 605 606 621 646 653 670 685
1000th Curzon number with base 6: 20717
 
Curzon numbers with base 8
1 14 35 44 72 74 77 129 131 137
144 149 150 185 200 219 236 266 284 285
299 309 336 357 381 386 390 392 402 414
420 441 455 459 470 479 500 519 527 536
557 582 600 602 617 639 654 674 696 735
1000th Curzon number with base 8: 22176
 
Curzon numbers with base 10
1 9 10 25 106 145 190 193 238 253
306 318 349 385 402 462 486 526 610 649
658 678 733 762 810 990 994 1033 1077 1125
1126 1141 1149 1230 1405 1422 1441 1485 1509 1510
1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
1000th Curzon number with base 10: 46845
</pre>
 
=={{header|Pascal}}==
Line 1,097 ⟶ 1,569:
46845</pre>
 
=={{header|OCamlPARI/GP}}==
{{trans|Mathematica/Wolfram_Language}}
<syntaxhighlight lang="ocaml">let modpow m =
<syntaxhighlight lang="PARI/GP">
let rec loop p b e =
/* Define the CurzonNumberQ function for base b */
if e land 1 = 0
powermod(a,k,n)=lift(Mod(a,n)^k)
then if e = 0 then p else loop p (b * b mod m) (e lsr 1)
CurzonNumberQ(b, n) = (powermod(b,n,b*n+1)== b*n);
else loop (p * b mod m) (b * b mod m) (e lsr 1)
in loop 1
 
/* Define a function to find Curzon numbers within a range for base b */
let is_curzon k n =
FindCurzonNumbers(b, maxrange) = {
let r = k * n in r = modpow (succ r) k n
local(val, res, i);
val = vector(maxrange);
res = [];
for(i = 1, maxrange,
if(CurzonNumberQ(b, i), res = concat(res, i));
);
return(Vec(res));
}
 
/* Select and display the first 50 Curzon numbers and the 1000th for base 2 */
let () =
val = FindCurzonNumbers(2, 100000);
List.iter (fun x ->
print(vector(50, i, val[i])); /* First 50 */
Seq.(ints 0 |> filter (is_curzon x) |> take 50 |> map string_of_int)
print(val[1000]); /* 1000th Curzon number */
|> List.of_seq |> String.concat " " |> Printf.printf "base %u:\n%s\n" x)
[2; 4; 6; 8; 10]
 
/* Select and display for base 4 */
let () =
val = FindCurzonNumbers(4, 100000);
List.iter (fun x ->
print(vector(50, i, val[i])); /* First 50 */
Seq.(ints 0 |> filter (is_curzon x) |> drop 999 |> take 1
print(val[1000]); /* 1000th Curzon number */
|> iter (Printf.printf "base %u (1000th): %u\n" x)))
 
[2; 4; 6; 8; 10]</syntaxhighlight>
/* Select and display for base 6 */
val = FindCurzonNumbers(6, 100000);
print(vector(50, i, val[i])); /* First 50 */
print(val[1000]); /* 1000th Curzon number */
 
/* Select and display for base 8 */
val = FindCurzonNumbers(8, 100000);
print(vector(50, i, val[i])); /* First 50 */
print(val[1000]); /* 1000th Curzon number */
 
/* Select and display for base 10 */
val = FindCurzonNumbers(10, 100000);
print(vector(50, i, val[i])); /* First 50 */
print(val[1000]); /* 1000th Curzon number */
</syntaxhighlight>
{{out}}
<pre>
[1, 2, 5, 6, 9, 14, 18, 21, 26, 29, 30, 33, 41, 50, 53, 54, 65, 69, 74, 78, 81, 86, 89, 90, 98, 105, 113, 114, 125, 134, 138, 141, 146, 153, 158, 165, 173, 174, 186, 189, 194, 198, 209, 210, 221, 230, 233, 245, 249, 254]
base 2:
8646
1 2 5 6 9 14 18 21 26 29 30 33 41 50 53 54 65 69 74 78 81 86 89 90 98 105 113 114 125 134 138 141 146 153 158 165 173 174 186 189 194 198 209 210 221 230 233 245 249 254
[1, 3, 7, 9, 13, 15, 25, 27, 37, 39, 43, 45, 49, 57, 67, 69, 73, 79, 87, 93, 97, 99, 105, 115, 127, 135, 139, 153, 163, 165, 169, 175, 177, 183, 189, 193, 199, 205, 207, 213, 219, 235, 249, 253, 255, 265, 267, 273, 277, 279]
base 4:
9375
1 3 7 9 13 15 25 27 37 39 43 45 49 57 67 69 73 79 87 93 97 99 105 115 127 135 139 153 163 165 169 175 177 183 189 193 199 205 207 213 219 235 249 253 255 265 267 273 277 279
[1, 6, 30, 58, 70, 73, 90, 101, 105, 121, 125, 146, 153, 166, 170, 181, 182, 185, 210, 233, 241, 242, 266, 282, 290, 322, 373, 381, 385, 390, 397, 441, 445, 446, 450, 453, 530, 557, 562, 585, 593, 601, 602, 605, 606, 621, 646, 653, 670, 685]
base 6:
20717
1 6 30 58 70 73 90 101 105 121 125 146 153 166 170 181 182 185 210 233 241 242 266 282 290 322 373 381 385 390 397 441 445 446 450 453 530 557 562 585 593 601 602 605 606 621 646 653 670 685
[1, 14, 35, 44, 72, 74, 77, 129, 131, 137, 144, 149, 150, 185, 200, 219, 236, 266, 284, 285, 299, 309, 336, 357, 381, 386, 390, 392, 402, 414, 420, 441, 455, 459, 470, 479, 500, 519, 527, 536, 557, 582, 600, 602, 617, 639, 654, 674, 696, 735]
base 8:
22176
1 14 35 44 72 74 77 129 131 137 144 149 150 185 200 219 236 266 284 285 299 309 336 357 381 386 390 392 402 414 420 441 455 459 470 479 500 519 527 536 557 582 600 602 617 639 654 674 696 735
[1, 9, 10, 25, 106, 145, 190, 193, 238, 253, 306, 318, 349, 385, 402, 462, 486, 526, 610, 649, 658, 678, 733, 762, 810, 990, 994, 1033, 1077, 1125, 1126, 1141, 1149, 1230, 1405, 1422, 1441, 1485, 1509, 1510, 1513, 1606, 1614, 1630, 1665, 1681, 1690, 1702, 1785, 1837]
base 10:
46845
1 9 10 25 106 145 190 193 238 253 306 318 349 385 402 462 486 526 610 649 658 678 733 762 810 990 994 1033 1077 1125 1126 1141 1149 1230 1405 1422 1441 1485 1509 1510 1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
base 2 (1000th): 8646
base 4 (1000th): 9375
base 6 (1000th): 20717
base 8 (1000th): 22176
base 10 (1000th): 46845
</pre>
 
Line 1,413 ⟶ 1,902:
1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
One thousandth: 46845</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
≪ → x n m
≪ '''IF''' OVER
'''THEN''' 1 1 n '''START''' x * m MOD '''NEXT'''
'''ELSE''' 1 '''END'''
≫ ≫ 'XnMOD' STO
≪ → k
≪ {} 1 '''WHILE''' OVER SIZE 50 < '''REPEAT'''
k OVER * 1 +
DUP2 k ROT ROT XnMOD 1 + SWAP MOD
'''IF''' NOT '''THEN''' SWAP OVER + SWAP '''END'''
1 +
'''END''' DROP
≫ ≫ 'QRZ50' STO
|
''( x n m -- (x^n mod m) )''
If n<>0...
then x^n mod m = (((x mod m) * x mod m) ... * x mod m)
else x^0 mod m = 1
''( k -- { 50 Curzon numbers } )''
Initialize
Get k*n + 1
Get k^n+1 mod k*n+1
If zero then add it to sequence
Increment n
|}
The following line of code delivers what is required:
2 QRZ50 10 QRZ50
{{out}}
<pre>
2: { 1 2 5 6 9 14 18 21 26 29 30 33 41 50 53 54 65 69 74 78 81 86 89 90 98 105 113 114 125 134 138 141 146 153 158 165 173 174 186 189 194 198 209 210 221 230 233 245 249 254 }
1: { 1 9 10 25 106 145 190 193 238 253 306 318 349 385 402 462 486 526 610 649 658 678 733 762 810 990 994 1033 1077 1125 1126 1141 1149 1230 1405 1422 1441 1485 1509 1510 1513 1606 1614 1630 1665 1681 1690 1702 1785 1837 }
</pre>
 
=={{header|Ruby}}==
Line 1,670 ⟶ 2,204:
 
=={{header|Wren}}==
===Version 1 (Embedded using GMP)===
{{libheader|Wren-gmp}}
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">/* curzon_numbersCurzon_numbers.wren */
 
import "./gmp" for Mpz
import "./seq" for Lst
import "./fmt" for Fmt
 
Line 1,692 ⟶ 2,225:
count = count + 1
if (count == 50) {
for (chunk in Lst.chunks(curzon50, 10)) Fmt.printtprint("$4d", chunkcurzon50, 10)
System.write("\nOne thousandth: ")
}
if (count == 1000) {
SystemFmt.print("$,d", n)
break
}
Line 1,709 ⟶ 2,242:
<pre>
The first 50 Curzon numbers using a base of 2:
1 2 5 6 9 14 18 21 26 29
30 33 41 50 53 54 65 69 74 78
81 86 89 90 98 105 113 114 125 134
138 141 146 153 158 165 173 174 186 189
194 198 209 210 221 230 233 245 249 254
 
One thousandth: 86468,646
 
The first 50 Curzon numbers using a base of 4:
1 3 7 9 13 15 25 27 37 39
43 45 49 57 67 69 73 79 87 93
97 99 105 115 127 135 139 153 163 165
169 175 177 183 189 193 199 205 207 213
219 235 249 253 255 265 267 273 277 279
 
One thousandth: 93759,375
 
The first 50 Curzon numbers using a base of 6:
1 6 30 58 70 73 90 101 105 121
125 146 153 166 170 181 182 185 210 233
241 242 266 282 290 322 373 381 385 390
397 441 445 446 450 453 530 557 562 585
593 601 602 605 606 621 646 653 670 685
 
One thousandth: 2071720,717
 
The first 50 Curzon numbers using a base of 8:
1 14 35 44 72 74 77 129 131 137
144 149 150 185 200 219 236 266 284 285
299 309 336 357 381 386 390 392 402 414
420 441 455 459 470 479 500 519 527 536
557 582 600 602 617 639 654 674 696 735
 
One thousandth: 2217622,176
 
The first 50 Curzon numbers using a base of 10:
1 9 10 25 106 145 190 193 238 253
306 318 349 385 402 462 486 526 610 649
658 678 733 762 810 990 994 1033 1077 1125
1126 1141 1149 1230 1405 1422 1441 1485 1509 1510
1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
 
One thousandth: 4684546,845
</pre>
===Version 2 (Wren-cli)===
{{trans|C++}}
{{libheader|Wren-math}}
Alternatively, using ''Int.modPow'' rather than ''GMP'' so it will run on Wren-cli, albeit about 6 times more slowly.
<syntaxhighlight lang="wren">import "./math" for Int
import "./fmt" for Fmt
 
var isCurzon = Fn.new { |n, k|
var r = k * n
return Int.modPow(k, n, r+1) == r
}
 
var k = 2
while (k <= 10) {
System.print("Curzon numbers with base %(k):")
var n = 1
var count = 0
while (count < 50) {
if (isCurzon.call(n, k)) {
Fmt.write("$4d ", n)
count = count + 1
if (count % 10 == 0) System.print()
}
n = n + 1
}
while (true) {
if (isCurzon.call(n, k)) count = count + 1
if (count == 1000) break
n = n + 1
}
Fmt.print("1,000th Curzon number with base $d: $,d\n", k, n)
k = k + 2
}</syntaxhighlight>
 
{{out}}
<pre>
As Version 1.
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">func ModPow(Base, Exp, Mod);
int Base, Exp, Mod, Result;
[if Mod = 1 then return 0;
Result:= 1;
Base:= rem(Base/Mod);
while Exp > 0 do
[if (Exp&1) = 1 then Result:= rem((Result*Base)/Mod);
Base:= rem((Base*Base) / Mod);
Exp:= Exp >> 1;
];
return Result;
];
 
func IsCurzon(N, K);
int N, K, R;
[R:= K * N;
return ModPow(K, N, R+1) = R;
];
 
int K, N, Count;
[K:= 2;
Format(5, 0);
while K <= 10 do
[Text(0, "Curzon numbers with base "); IntOut(0, K); CrLf(0);
N:= 1; Count:= 0;
while Count < 50 do
[if IsCurzon(N, K) then
[RlOut(0, float(N));
Count:= Count+1;
if rem(Count/10) = 0 then CrLf(0);
];
N:= N+1;
];
K:= K+2;
];
]</syntaxhighlight>
{{out}}
<pre>
Curzon numbers with base 2
1 2 5 6 9 14 18 21 26 29
30 33 41 50 53 54 65 69 74 78
81 86 89 90 98 105 113 114 125 134
138 141 146 153 158 165 173 174 186 189
194 198 209 210 221 230 233 245 249 254
Curzon numbers with base 4
1 3 7 9 13 15 25 27 37 39
43 45 49 57 67 69 73 79 87 93
97 99 105 115 127 135 139 153 163 165
169 175 177 183 189 193 199 205 207 213
219 235 249 253 255 265 267 273 277 279
Curzon numbers with base 6
1 6 30 58 70 73 90 101 105 121
125 146 153 166 170 181 182 185 210 233
241 242 266 282 290 322 373 381 385 390
397 441 445 446 450 453 530 557 562 585
593 601 602 605 606 621 646 653 670 685
Curzon numbers with base 8
1 14 35 44 72 74 77 129 131 137
144 149 150 185 200 219 236 266 284 285
299 309 336 357 381 386 390 392 402 414
420 441 455 459 470 479 500 519 527 536
557 582 600 602 617 639 654 674 696 735
Curzon numbers with base 10
1 9 10 25 106 145 190 193 238 253
306 318 349 385 402 462 486 526 610 649
658 678 733 762 810 990 994 1033 1077 1125
1126 1141 1149 1230 1405 1422 1441 1485 1509 1510
1513 1606 1614 1630 1665 1681 1690 1702 1785 1837
</pre>
1,969

edits

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