Perl 对大数字几乎没有问题,如果您的数字真的很大,只需use bignum
. 这透明地启用了无限精度的算术。
您的号码 21767823360 需要大约 35 位。Myperl
是用 64 位整数构建的(请参阅perl -v
以检查您的支持),因此您的数字对我来说不是“太大”。
将数字转换为 base-n 的算法很简单:
# pseudocode
let "digits" be the array containing all the digits of our representation.
# the size of digits is the base of our new representation
# the digits are sorted in ascending order.
#digits[0] is zero.
var "n" is the number we want to represent.
var "size" is the number of digits of the new representation.
# floor(ln(n)/ln(digits.size))
var "representation" is the empty string.
while size >= 0:
representation ← representation.concat(digits[n / digits.length ^ size]).
n ← n.modulo(digits.length ^ size).
size ← size - 1.
return representation.
示例 Perl:
#!/usr/bin/perl
use strict; use warnings;
use Carp;
sub base_n {
my ($number, $base, $max_digits, $pad) = @_;
defined $number or croak "Undefined number for base_n";
$number == int $number and $number >= 0
or croak "The number has to be a natural number for base_n";
defined $base or croak "Undefined base for base_n";
$base == int $base and $base > 0
or croak "The base has to be a positive integer for base_n";
my @digits = (0 .. 9, "A" .. "Z");
$base <= @digits or croak "base_n can only convert to base-" . @digits . " max.";
@digits = @digits[0 .. $base - 1];
my $size = $number ? int(log($number) / log($base)) : 0; # avoid log(0)
if (defined $max_digits) {
$size < $max_digits
or croak "The number $number is too large for $max_digits digits in base $base.";
$size = $max_digits - 1 if $pad;
}
my $representation = "";
while ($size >= 0) {
$representation .= $digits[$number / @digits**$size];
$number %= @digits**$size;
$size--;
}
if (wantarray) {
my $checksum = substr $representation, -1;
return $representation, $checksum;
} else {
return $representation;
}
}
相应的(但不完整的)单元测试:
use Test::More;
my $n = 21767823360;
ok "A000000" eq base_n($n => 36), "simple";
ok "A000000" eq base_n($n => 36, 8), "valid constraint";
ok "0A000000" eq base_n($n => 36, 8, 1), "padding";
ok ! eval { base_n($n => 36, 6); 1 }, "invalid constraint";
ok "0" eq (base_n($n => 36))[1], "checksum (1)";
ok "A" eq (base_n($n+10 => 36))[1], "checksum (2)";
ok "0" eq base_n(0 => 36), "zero: simple";
ok "0"x8 eq base_n(0 => 36, 8, 1), "zero: padding";
ok ! eval { base_n($n => 0.7); 1 }, "invalid base";
ok ! eval { base_n(0.7 => 36); 1 }, "invalid number";
ok $n == base_n($n => 10), "round-trip safety";
ok $n eq base_n($n => 10, length $n, 1), "round-trip safety: padding";
done_testing;