Skip to content

Commit

Permalink
better debug print for inverse interpolate
Browse files Browse the repository at this point in the history
  • Loading branch information
danaj committed May 20, 2024
1 parent 95f1922 commit d01cd3d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ t/26-isfundamental.t
t/26-isgaussianprime.t
t/26-isodd.t
t/26-isomegaprime.t
t/26-isperfectnumber.t
t/26-ispower.t
t/26-issemiprime.t
t/26-issquarefree.t
Expand Down
3 changes: 2 additions & 1 deletion inverse_interpolate.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ static UV _inverse_interpolate(UV lo, UV hi, UV n,
UV x0 = lo, x1 = lo + ((hi-lo)>>1); /* x2 = hi */
UV rx1 = CALLBACK(x1);
IV fx0 = rlo-n, fx1 = rx1-n, fx2=rhi-n+1;
if(_dbgprint)printf(" 2s lo %lu mid %lu hi %lu (%lu)\n", lo, x1, hi, (rx1>n) ? rx1-n : n-rx1);

double pos = ((double)(x1-x0) * (double)fx1)
/ sqrtl((double)fx1 * (double)fx1 - (double)fx0 * (double)fx2);
Expand All @@ -141,14 +142,14 @@ static UV _inverse_interpolate(UV lo, UV hi, UV n,
else { lo = x1; rlo = rx1; }
} else {
UV rx3 = CALLBACK(x3);
if(_dbgprint)printf(" 2S lo %lu mid %lu hi %lu (%lu)\n", lo, x3, hi, (rx3>n) ? rx3-n : n-rx3);
/* Swap if needed to have: [lo x1 x3 hi] */
if (rx1 > rx3) { UV t=x1; x1=x3; x3=t; t=rx1; fx1=rx3; rx3=t; }
if (rx1 >= n) { hi = x1; rhi = rx1; }
else if (rx3 >= n) { lo = x1; rlo = rx1; hi = x3; rhi = rx3; }
else { lo = x3; rlo = rx3; }
}
MPUassert(rlo < n && rhi >= n, "interpolation: Ridder step error");
if(_dbgprint)printf(" 2s lo %lu mid %lu hi %lu (%lu)\n", lo, mid, hi, rhi-n);
}

/* Step 3. Binary search. */
Expand Down
22 changes: 22 additions & 0 deletions t/26-isperfectnumber.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env perl
use strict;
use warnings;

use Test::More;
use Math::Prime::Util qw/is_perfect_number/;

#my $extra = defined $ENV{EXTENDED_TESTING} && $ENV{EXTENDED_TESTING};
#my $usexs = Math::Prime::Util::prime_get_config->{'xs'};
#my $usegmp = Math::Prime::Util::prime_get_config->{'gmp'};

my @true = (qw/8128 33550336 8589869056 137438691328 2305843008139952128 2658455991569831744654692615953842176/);
my @false = (qw/8505 12285 19845 28665 31185 198585576189 8 32 2096128 35184367894528 144115187807420416 9444732965670570950656/);


plan tests => 1 + 2;
;

is_deeply([grep { is_perfect_number($_) } -10 .. 500], [6,28,496], "is_perfect_number(-10 .. 500)");

is_deeply([map { is_perfect_number($_) } @true], [map { 1 } @true], "perfect: [@true]");
is_deeply([map { is_perfect_number($_) } @false], [map { 0 } @false], "not perfect: [@false]");

0 comments on commit d01cd3d

Please sign in to comment.