use strict;
use warnings;
use Math::AnyNum 'ipow';

sub curzon {
    my($base,$cnt) = @_;
    my($n,@C) = 0;
    while (++$n) {
        push @C, $n if 0 == (ipow($base,$n) + 1) % ($base * $n + 1);
        return @C if $cnt == @C;
    }
}

my $upto = 50;
for my $k (<2 4 6 8 10>) {
    my @C = curzon($k,1000);
    print "First $upto Curzon numbers using a base of $k:\n" .
    (sprintf "@{['%5d' x $upto]}", @C[0..$upto-1]) =~ s/(.{125})/$1\n/gr;
    print "Thousandth: $C[-1]\n\n";
}