- 论坛徽章:
- 7
|
v3~ 
my ( $x, $y ) = ( 2, 1500000 ); # 1 秒
感谢 Eratosthenes, yestreenstars 2 位大牛的指点。- #!/usr/bin/perl
- # my ( $x, $y ) = do { print 'x, y: '; <> =~ /(\d+)/g };
- my ( $x, $y ) = ( 2, 1500000 );
- my ( $y2, $sqrt, $count ) = ( $y / 2, sqrt $y );
- my @prime = (1) x ( $y2 + 1 );
- for my $i ( 2 .. sqrt $y2 ) {
- next unless $prime[$i];
- my $j = $i * $i;
- undef $prime[$j] or $j += $i while $j <= $y2;
- }
- @prime = grep $prime[$_], 2 .. $#prime;
- for my $i ( 0 .. $#prime ) {
- last if $i > $sqrt;
- for my $j ( $i .. $#prime ) {
- my $sp = $prime[$i] * $prime[$j];
- $sp > $y ? last : $sp >= $x && $count++;
- }
- }
- print $count, $/;
复制代码 |
|