File: moebius-mertens.pl

package info (click to toggle)
libmath-prime-util-perl 0.73-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 2,800 kB
  • sloc: perl: 24,676; ansic: 11,471; python: 24; makefile: 18
file content (33 lines) | stat: -rwxr-xr-x 870 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env perl
use strict;
use warnings;
$| = 1;  # fast pipes

use Math::Prime::Util qw/moebius mertens vecsum/;

my $limit = shift || 1_000_000;

print "Calculating moebius from 1 to $limit...";
my @mu = map { moebius($_) } 1 .. $limit;
print "...";
unshift @mu, 0;
print "...done\n";

while (1) {
  my $beg = 1 + int(rand($limit));
  my $end = 1 + int(rand($limit));
  ($beg,$end) = ($end,$beg) if $beg > $end;

  # Does moebius range return the same values?
  my @mu_range = @mu[ $beg .. $end ];
  my @mobius = moebius($beg,$end);

  my $mu_sum = vecsum(@mu_range);
  my $mo_sum = vecsum(@mobius);
  my $mert_sum = mertens($end) - mertens($beg-1);
  warn "\nbeg $beg  end $end  sum $mu_sum  range sum $mo_sum\n"
       unless $mu_sum == $mo_sum;
  warn "\nbeg $beg  end $end  sum $mu_sum  mertsum $mert_sum\n"
       unless $mu_sum == $mert_sum;
  print ".";
}