Curzon numbers: Difference between revisions
Perl: fast modular exponentiation
m (do one addition less) |
(Perl: fast modular exponentiation) |
||
Line 1,098:
<syntaxhighlight lang="perl">use strict;
use warnings;
sub modpow {
my($b,$e,$m) = @_;
my $p = 1;
while ($e) {
$p = $p * $b % $m if $e & 1;
$b = $b * $b % $m;
$e >>= 1
}
$p
}
sub curzon {
Line 1,104 ⟶ 1,114:
my($n,@C) = 0;
while (++$n) {
push @C, $n if $r == modpow $base, $n, $r + 1;
return @C if $cnt == @C;
}
Line 1,111 ⟶ 1,122:
my $upto = 50;
for my $k (<2 4 6 8 10>) {
my @C = curzon
print "First $upto Curzon numbers using a base of $k:\n" .
(sprintf "@{['%5d' x $upto]}", @C[0..$upto-1]) =~ s/
}</syntaxhighlight>
{{out}}
<pre>
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 Thousandth: 8646
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
219 235 249 253 255 265 267 273 277 279 Thousandth: 9375
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
593 601 602 605 606 621 646 653 670 685 Thousandth: 20717
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
557 582 600 602 617 639 654 674 696 735 Thousandth: 22176
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
1513 1606 1614 1630 1665 1681 1690 1702 1785 1837 Thousandth: 46845
</pre>
=={{header|Phix}}==
|