Curzon numbers: Difference between revisions

m
syntax highlighting fixup automation
m (Python example)
m (syntax highlighting fixup automation)
Line 33:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">curzon?: function [n,base]->
zero? (inc base^n) % inc base*n
 
Line 63:
print ["\n1000th Curzon with base" withBase "=" oneThousandth withBase]
print ""
]</langsyntaxhighlight>
 
{{out}}
Line 114:
=={{header|C++}}==
{{libheader|GMP}}
<langsyntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <vector>
Line 147:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 195:
=={{header|Factor}}==
{{works with|Factor|0.99 2022-04-03}}
<langsyntaxhighlight lang="factor">USING: grouping interpolate io kernel make math math.functions
prettyprint ranges sequences ;
 
Line 209:
curzon 10 group simple-table. ;
 
2 10 2 <range> [ curzon. nl ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 250:
=={{header|FreeBASIC}}==
===Normal basic===
<langsyntaxhighlight lang="freebasic">' limit: k * n +1 must be smaller then 2^32-1
 
Function pow_mod(b As ULongInt, power As ULongInt, modulus As ULongInt) As ULongInt
Line 291:
 
Next
Sleep</langsyntaxhighlight>{{out}}<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
Line 342:
===GMP version===
{{libheader|GMP}}
<langsyntaxhighlight lang="freebasic">#include once "gmp.bi"
 
Dim As Longint t = Len(__mpz_struct)
Line 371:
Loop
Next k
Sleep</langsyntaxhighlight>
 
=={{header|Go}}==
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 421:
fmt.Println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 479:
 
'''Preliminaries'''
<langsyntaxhighlight lang="jq"># To take advantage of gojq's arbitrary-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
Line 489:
n;
 
def printRows($m): _nwise($m) | map(lpad(5)) | join("");</langsyntaxhighlight>
 
'''The task'''
<langsyntaxhighlight lang="jq">def isCurzon($n; $k):
($k | power($n) + 1) % ($k * $n + 1) == 0;
 
Line 509:
"";
printcurzons(2; 10; 1000)</langsyntaxhighlight>
{{out}}
<pre>
Line 540:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">isCurzon(n, k) = (BigInt(k)^n + 1) % (k * n + 1) == 0
 
function printcurzons(klow, khigh)
Line 556:
 
printcurzons(2, 10)
</langsyntaxhighlight>{{out}}
<pre>
Curzon numbers with k = 2:
Line 584:
 
It isn't clear why the task description says "generalized Curzon numbers only exist for even base integers." If k >= 3 is an odd base, then, besides the trivial solution n = 1, it can be checked that n = k^(k-1) is a Curzon number according to the given definition. It seems from the output below that Curzon numbers with an odd base are much scarcer than those with an even base.
<langsyntaxhighlight lang="pascal">
program CurzonNumbers;
uses SysUtils;
Line 653:
ListCurzonNumbers(11);
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 714:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[CurzonNumberQ]
CurzonNumberQ[b_Integer][n_Integer]:=PowerMod[b,n,b n+1]==b n
val=Select[Range[100000],CurzonNumberQ[2]];
Line 734:
val=Select[Range[100000],CurzonNumberQ[10]];
Take[val,50]
val[[1000]]</langsyntaxhighlight>
{{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}
Line 757:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use Math::AnyNum 'ipow';
Line 776:
(sprintf "@{['%5d' x $upto]}", @C[0..$upto-1]) =~ s/(.{125})/$1\n/gr;
print "Thousandth: $C[-1]\n\n";
}</langsyntaxhighlight>
{{out}}
<pre>First 50 Curzon numbers using a base of 2:
Line 804:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 828:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 858:
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">def is_Curzon(n, k):
return (k**n + 1) % (k * n + 1) == 0
 
Line 871:
print(f'{c: 5,}', end='\n' if (i + 1) % 25 == 0 else '')
print(f' Thousandth Curzon with k = {k}: {curzons[999]}.\n')
</langsyntaxhighlight>{{out}}
<pre>
Curzon numbers with k = 2:
Line 901:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ number$
space 4 of swap join
-5 split nip echo$ ] is rjust ( n --> )
Line 933:
say " ... "
-1 peek echo cr cr ]
</syntaxhighlight>
</lang>
 
{{out}}
Line 979:
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>sub curzon ($base) { lazy (1..∞).hyper.map: { $_ if (exp($_, $base) + 1) %% ($base × $_ + 1) } };
 
for <2 4 6 8 10> {
Line 986:
$curzon[^50].batch(25)».fmt("%4s").join("\n") ~
"\nOne thousandth: " ~ $curzon[999]
}</langsyntaxhighlight>
{{out}}
<pre>First 50 Curzon numbers using a base of 2:
Line 1,014:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">// [dependencies]
// rug = "1.15.0"
 
Line 1,045:
println!("1000th Curzon number with base {k}: {n}\n");
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,092:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_curzon(n, k) {
powmod(k, n, k*n + 1).is_congruent(-1, k*n + 1) && (n > 0)
}
Line 1,100:
say 50.by {|n| is_curzon(n, k) }.join(' ')
say ("1000th term: ", 1000.th {|n| is_curzon(n,k) })
}</langsyntaxhighlight>
 
{{out}}
Line 1,127:
=={{header|Vlang}}==
{{trans|go}}
<langsyntaxhighlight lang="vlang">import math.big
 
fn main() {
Line 1,167:
println('')
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,221:
{{libheader|Wren-seq}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">/* curzon_numbers.wren */
 
import "./gmp" for Mpz
Line 1,252:
}
System.print()
}</langsyntaxhighlight>
 
{{out}}
10,327

edits