最大的指数
作者:Andrei Osipov
https://projecteuler.net/problem=99
比较以指数形式表示的两个数,例如 2^11 和 3^7 并不困难,任何计算器都可以确认 2^11 = 2048 < 3^7 = 2187。
但是,确认 632382^518061 > 519432^525806 则要困难得多,因为这两个数都包含超过三百万位数字。
使用 base_exp.txt https://projecteuler.net/project/resources/p099_base_exp.txt,一个 22K 的文本文件,其中包含一千行,每行有一对底数/指数,确定哪一行的数值最大。
注意:文件中的前两行表示上面示例中的数字。
源代码: prob099-andreoss.pl
use v6;
class BaseExp {
has $.base;
has $.exp;
has $.line;
method comparable {
$.exp * $.base.log;
}
}
multi infix:«cmp»(BaseExp $a, BaseExp $b) {
$a.comparable <=> $b.comparable;
}
multi my-max($a, $b where $a cmp $b ~~ More) { $a }
multi my-max($a, $b) { $b }
sub MAIN(:$file = $*SPEC.catdir($*PROGRAM-NAME.IO.dirname, 'base_exp.txt'),
) {
die "'$file' is missing" unless $file.IO.e ;
my $n = 1 ;
say .line for [[&my-max]] do for $file.IO.lines -> $l {
my ($base, $exp) = $l.split: /','/;
BaseExp.new(base => $base,
exp => $exp,
line => $n++);
}
}
Perl 6 示例