[libmath-prime-util-perl] 11/23: Streamline some expensive tests, move bignum Zeta/R to separate file

Partha P. Mukherjee ppm-guest at moszumanska.debian.org
Thu May 21 18:45:55 UTC 2015


This is an automated email from the git hooks/post-receive script.

ppm-guest pushed a commit to annotated tag v0.14
in repository libmath-prime-util-perl.

commit e0e6f21da80d898e1faf029f5324cd2e54dd3ac3
Author: Dana Jacobsen <dana at acm.org>
Date:   Fri Nov 23 22:04:28 2012 -0800

    Streamline some expensive tests, move bignum Zeta/R to separate file
---
 MANIFEST                            |   1 +
 lib/Math/Prime/Util.pm              |   4 +-
 lib/Math/Prime/Util/PP.pm           | 418 +++++++----------------------
 lib/Math/Prime/Util/ZetaBigFloat.pm | 509 ++++++++++++++++++++++++++++++++++++
 t/14-nthprime.t                     |  11 +-
 t/15-probprime.t                    | 200 +++++++-------
 t/51-primearray.t                   |  85 +++---
 t/80-pp.t                           | 110 ++++----
 t/81-bignum.t                       |  90 +++++--
 9 files changed, 890 insertions(+), 538 deletions(-)

diff --git a/MANIFEST b/MANIFEST
index ef85997..66eb448 100644
--- a/MANIFEST
+++ b/MANIFEST
@@ -3,6 +3,7 @@ lib/Math/Prime/Util.pm
 lib/Math/Prime/Util/MemFree.pm
 lib/Math/Prime/Util/PrimeArray.pm
 lib/Math/Prime/Util/PP.pm
+lib/Math/Prime/Util/ZetaBigFloat.pm
 LICENSE
 Makefile.PL
 MANIFEST
diff --git a/lib/Math/Prime/Util.pm b/lib/Math/Prime/Util.pm
index f257e90..5eab9a2 100644
--- a/lib/Math/Prime/Util.pm
+++ b/lib/Math/Prime/Util.pm
@@ -1437,7 +1437,9 @@ sub LogarithmicIntegral {
     }
     return 1.045163780117492784844588889194613136522615578151 if $n == 2;
   }
-  ExponentialIntegral(log($n));
+  return Math::Prime::Util::PP::LogarithmicIntegral($n)
+    if defined $bignum::VERSION || ref($n) eq 'Math::BigFloat' || !$_Config{'xs'};
+  return _XS_LogarithmicIntegral($n);
 }
 
 #############################################################################
diff --git a/lib/Math/Prime/Util/PP.pm b/lib/Math/Prime/Util/PP.pm
index e3d154e..d047d9f 100644
--- a/lib/Math/Prime/Util/PP.pm
+++ b/lib/Math/Prime/Util/PP.pm
@@ -1352,12 +1352,13 @@ sub ExponentialIntegral {
     $val = $sum;
   } else {
     # Asymptotic divergent series
-    $val = exp($x) / $x;
-    $y = 1.0-$c; $t = $sum+$y; $c = ($t-$sum)-$y; $sum = $t;
+    my $invx = 1.0 / $x;
+    $val = exp($x) * $invx;
+    $sum = 1.0;
     my $term = 1.0;
     for my $n (1 .. 200) {
       my $last_term = $term;
-      $term *= $n/$x;
+      $term *= $n*$invx;
       last if $term < $tol;
       if ($term < $last_term) {
         $y = $term-$c; $t = $sum+$y; $c = ($t-$sum)-$y; $sum = $t;
@@ -1377,358 +1378,135 @@ sub LogarithmicIntegral {
   return 0+(-Infinity) if $x == 1;
   return $_const_li2 if $x == 2;
   croak "Invalid input to LogarithmicIntegral:  x must be > 0" if $x <= 0;
-  ExponentialIntegral(log($x));
-}
 
-# Riemann Zeta function for integers, used for computing Riemann R
-# So many terms and digits are used so we can quickly do bignum R.
-my @_Riemann_Zeta_Table = (
-  '0.64493406684822643647241516664602518921894990',   # zeta(2) - 1
-  '0.20205690315959428539973816151144999076498629',
-  '0.082323233711138191516003696541167902774750952',
-  '0.036927755143369926331365486457034168057080920',
-  '0.017343061984449139714517929790920527901817490',
-  '0.0083492773819228268397975498497967595998635606',
-  '0.0040773561979443393786852385086524652589607906',
-  '0.0020083928260822144178527692324120604856058514',
-  '0.00099457512781808533714595890031901700601953156',
-  '0.00049418860411946455870228252646993646860643576',
-  '0.00024608655330804829863799804773967096041608846',
-  '0.00012271334757848914675183652635739571427510590',
-  '0.000061248135058704829258545105135333747481696169',
-  '0.000030588236307020493551728510645062587627948707',
-  '0.000015282259408651871732571487636722023237388990',
-  '0.0000076371976378997622736002935630292130882490903',
-  '0.0000038172932649998398564616446219397304546972190',
-  '0.0000019082127165539389256569577951013532585711448',
-  '0.00000095396203387279611315203868344934594379418741',
-  '0.00000047693298678780646311671960437304596644669478',
-  '0.00000023845050272773299000364818675299493504182178',
-  '0.00000011921992596531107306778871888232638725499778',
-  '0.000000059608189051259479612440207935801227503918837',
-  '0.000000029803503514652280186063705069366011844730920',
-  '0.000000014901554828365041234658506630698628864788168',
-  '0.0000000074507117898354294919810041706041194547190319',
-  '0.0000000037253340247884570548192040184024232328930593',
-  '0.0000000018626597235130490064039099454169480616653305',
-  '0.00000000093132743241966818287176473502121981356795514',
-  '0.00000000046566290650337840729892332512200710626918534',
-  '0.00000000023283118336765054920014559759404950248298228',
-  '0.00000000011641550172700519775929738354563095165224717',
-  '0.000000000058207720879027008892436859891063054173122605',
-  '0.000000000029103850444970996869294252278840464106981987',
-  '0.000000000014551921891041984235929632245318420983808894',
-  '0.0000000000072759598350574810145208690123380592648509256',
-  '0.0000000000036379795473786511902372363558732735126460284',
-  '0.0000000000018189896503070659475848321007300850305893096',
-  '0.00000000000090949478402638892825331183869490875386000099',
-  '0.00000000000045474737830421540267991120294885703390452991',
-  '0.00000000000022737368458246525152268215779786912138298220',
-  '0.00000000000011368684076802278493491048380259064374359028',
-  '0.000000000000056843419876275856092771829675240685530571589',
-  '0.000000000000028421709768893018554550737049426620743688265',
-  '0.000000000000014210854828031606769834307141739537678698606',
-  '0.0000000000000071054273952108527128773544799568000227420436',
-  '0.0000000000000035527136913371136732984695340593429921456555',
-  '0.0000000000000017763568435791203274733490144002795701555086',
-  '0.00000000000000088817842109308159030960913863913863256088715',
-  '0.00000000000000044408921031438133641977709402681213364596031',
-  '0.00000000000000022204460507980419839993200942046539642366543',
-  '0.00000000000000011102230251410661337205445699213827024832229',
-  '0.000000000000000055511151248454812437237365905094302816723551',
-  '0.000000000000000027755575621361241725816324538540697689848904',
-  '0.000000000000000013877787809725232762839094906500221907718625',
-  '0.0000000000000000069388939045441536974460853262498092748358742',
-  '0.0000000000000000034694469521659226247442714961093346219504706',
-  '0.0000000000000000017347234760475765720489729699375959074780545',
-  '0.00000000000000000086736173801199337283420550673429514879071415',
-  '0.00000000000000000043368086900206504874970235659062413612547801',
-  '0.00000000000000000021684043449972197850139101683209845761574010',
-  '0.00000000000000000010842021724942414063012711165461382589364744',
-  '0.000000000000000000054210108624566454109187004043886337150634224',
-  '0.000000000000000000027105054312234688319546213119497764318887282',
-  '0.000000000000000000013552527156101164581485233996826928328981877',
-  '0.0000000000000000000067762635780451890979952987415566862059812586',
-  '0.0000000000000000000033881317890207968180857031004508368340311585',
-  '0.0000000000000000000016940658945097991654064927471248619403036418',
-  '0.00000000000000000000084703294725469983482469926091821675222838642',
-  '0.00000000000000000000042351647362728333478622704833579344088109717',
-  '0.00000000000000000000021175823681361947318442094398180025869417612',
-  '0.00000000000000000000010587911840680233852265001539238398470699902',
-  '0.000000000000000000000052939559203398703238139123029185055866375629',
-  '0.000000000000000000000026469779601698529611341166842038715592556134',
-  '0.000000000000000000000013234889800848990803094510250944989684323826',
-  '0.0000000000000000000000066174449004244040673552453323082200147137975',
-  '0.0000000000000000000000033087224502121715889469563843144048092764894',
-  '0.0000000000000000000000016543612251060756462299236771810488297723589',
-  '0.00000000000000000000000082718061255303444036711056167440724040096811',
-  '0.00000000000000000000000041359030627651609260093824555081412852575873',
-  '0.00000000000000000000000020679515313825767043959679193468950443365312',
-  '0.00000000000000000000000010339757656912870993284095591745860911079606',
-  '0.000000000000000000000000051698788284564313204101332166355512893608164',
-  '0.000000000000000000000000025849394142282142681277617708450222269121159',
-  '0.000000000000000000000000012924697071141066700381126118331865309299779',
-  '0.0000000000000000000000000064623485355705318034380021611221670660356864',
-  '0.0000000000000000000000000032311742677852653861348141180266574173608296',
-  '0.0000000000000000000000000016155871338926325212060114057052272720509148',
-  '0.00000000000000000000000000080779356694631620331587381863408997398684847',
-  '0.00000000000000000000000000040389678347315808256222628129858130379479700',
-  '0.00000000000000000000000000020194839173657903491587626465673047518903728',
-  '0.00000000000000000000000000010097419586828951533619250700091044144538432',
-  '0.000000000000000000000000000050487097934144756960847711725486604360898735',
-  '0.000000000000000000000000000025243548967072378244674341937966175648398693',
-  '0.000000000000000000000000000012621774483536189043753999660777148710632765',
-  '0.0000000000000000000000000000063108872417680944956826093943332037500694712',
-  '0.0000000000000000000000000000031554436208840472391098412184847972814371270',
-  '0.0000000000000000000000000000015777218104420236166444327830159601782237092',
-);
+  $x = new Math::BigFloat "$x" if defined $bignum::VERSION && ref($x) ne 'Math::BigFloat';
+  my $logx = log($x);
 
-# Select n = 55, good for 46ish digits of accuracy.
-my $_Borwein_n = 55;
-my @_Borwein_dk = (
-  '1',
-  '6051',
-  '6104451',
-  '2462539971',
-  '531648934851',
-  '71301509476803',
-  '6504925195108803',
-  '429144511928164803',
-  '21392068013887742403',
-  '832780518854440804803',
-  '25977281563850106233283',
-  '662753606729324750201283',
-  '14062742362385399866745283',
-  '251634235316509414702211523',
-  '3841603462178827861104812483',
-  '50535961819850087101900022211',
-  '577730330374203014014104003011',
-  '5782012706584553297863989289411',
-  '50984922488525881477588707205571',
-  '398333597655022403279683908035011',
-  '2770992240330783259897072664469955',
-  '17238422988353715312442126057365955',
-  '96274027751337344115352100618133955',
-  '484350301573059857715727453968687555',
-  '2201794236784087151947175826243477955',
-  '9068765987529892610841571032285864387',
-  '33926582279822401059328069515697217987',
-  '115535262182820447663793177744255246787',
-  '358877507711760077538925500462137369027',
-  '1018683886695854101193095537014797787587',
-  '2646951832121008166346437186541363159491',
-  '6306464665572570713623910486640730071491',
-  '13799752848354341643763498672558481367491',
-  '27780237373991939435100856211039992177091',
-  '51543378762608611361377523633779417047491',
-  '88324588911945720951614452340280439890371',
-  '140129110249040241501243929391690331218371',
-  '206452706984942815385219764876242498642371',
-  '283527707823296964404071683165658912154051',
-  '364683602811933600833512164561308162744771',
-  '441935796522635816776473230396154031661507',
-  '508231717051242054487234759342047053767107',
-  '559351463001010719709990637083458540691907',
-  '594624787018881191308291683229515933311427',
-  '616297424973434835299724300924272199623107',
-  '628083443816135918099559567176252011864515',
-  '633714604276098212796088600263676671320515',
-  '636056734158553360761837806887547188568515',
-  '636894970116484676875895417679248215794115',
-  '637149280289288581322870186196318041432515',
-  '637213397278310656625865036925470191411651',
-  '637226467136294189739463288384528579584451',
-  '637228536449134002301138291602841035366851',
-  '637228775173095037281299181461988671775171',
-  '637228793021615488494769154535569803469251',
-  '637228793670652595811622608101881844621763',
-);
-# "An Efficient Algorithm for the Riemann Zeta Function", Borwein, 1991.
-# About 1.3n terms are needed for n digits of accuracy.
-sub _Recompute_Dk {
-  my $nterms = shift;
-  $_Borwein_n = $nterms;
-  @_Borwein_dk = ();
-  foreach my $k (0 .. $nterms) {
-    my $dsum = Math::BigFloat->bzero;
-    $dsum->accuracy(2*$_Borwein_n);
-    my $n = Math::BigInt->new($nterms-1)->bfac;
-    my $d = Math::BigInt->new($nterms)->bfac;
-    foreach my $i (0 .. $k) {
-      my $term = Math::BigFloat->bone;
-      $term->accuracy(2*$_Borwein_n);
-      $term->bmul($n)->bdiv($d);
-      $dsum += $term;
-      $n->bmul($nterms+$i)->bmul(4);
-      $d->bdiv($nterms-$i)->bmul(2*$i+1)->bmul(2*$i+2);
+  # Do divergent series here for big inputs.  Common for big pc approximations.
+  if ($x > 1e16) {
+    my $tol = 1e-16;
+    my $invx = 1.0 / $logx;
+    my $val = $x * $invx;
+    my $sum = 1.0;
+    my $term = 1.0;
+    for my $n (1 .. 200) {
+      my $last_term = $term;
+      $term *= $n * $invx;
+      last if $term < $tol;
+      if ($term < $last_term) {
+        $sum += $term;
+      } else {
+        $sum -= ($last_term/3);
+        last;
+      }
     }
-    my $dk = ($nterms * $dsum + 1e-20)->as_int;
-    $_Borwein_dk[$k] = $dk;
-    #print  "  '$dk',\n";
+    $val *= $sum;
+    return $val;
   }
+  ExponentialIntegral($logx);
 }
 
-sub RiemannZeta {
-  my($x, $tol) = @_;
-
-  if (!defined $bignum::VERSION && ref($x) !~ /^Math::Big/) {
-    return 0.0 + $_Riemann_Zeta_Table[int($x)-2]
-      if $x == int($x) && defined $_Riemann_Zeta_Table[int($x)-2];
-    $tol = 1e-16 unless defined $tol;
-    my($y, $t);
-    my $sum = 0.0;
-    my $c = 0.0;
+# Riemann Zeta function for native integers.
+my @_Riemann_Zeta_Table = (
+  0.6449340668482264364724151666460251892,  # zeta(2) - 1
+  0.2020569031595942853997381615114499908,
+  0.0823232337111381915160036965411679028,
+  0.0369277551433699263313654864570341681,
+  0.0173430619844491397145179297909205279,
+  0.0083492773819228268397975498497967596,
+  0.0040773561979443393786852385086524653,
+  0.0020083928260822144178527692324120605,
+  0.0009945751278180853371459589003190170,
+  0.0004941886041194645587022825264699365,
+  0.0002460865533080482986379980477396710,
+  0.0001227133475784891467518365263573957,
+  0.0000612481350587048292585451051353337,
+  0.0000305882363070204935517285106450626,
+  0.0000152822594086518717325714876367220,
+  0.0000076371976378997622736002935630292,
+  0.0000038172932649998398564616446219397,
+  0.0000019082127165539389256569577951013,
+  0.0000009539620338727961131520386834493,
+  0.0000004769329867878064631167196043730,
+  0.0000002384505027277329900036481867530,
+  0.0000001192199259653110730677887188823,
+  0.0000000596081890512594796124402079358,
+  0.0000000298035035146522801860637050694,
+  0.0000000149015548283650412346585066307,
+  0.0000000074507117898354294919810041706,
+  0.0000000037253340247884570548192040184,
+  0.0000000018626597235130490064039099454,
+  0.0000000009313274324196681828717647350,
+  0.0000000004656629065033784072989233251,
+  0.0000000002328311833676505492001455976,
+  0.0000000001164155017270051977592973835,
+  0.0000000000582077208790270088924368599,
+  0.0000000000291038504449709968692942523,
+  0.0000000000145519218910419842359296322,
+  0.0000000000072759598350574810145208690,
+  0.0000000000036379795473786511902372363,
+  0.0000000000018189896503070659475848321,
+  0.0000000000009094947840263889282533118,
+);
 
-    for my $k (2 .. 1000000) {
-      my $term = (2*$k+1) ** -$x;
-      $y = $term-$c; $t = $sum+$y; $c = ($t-$sum)-$y; $sum = $t;
-      last if $term < abs($tol*$sum);
-    }
-    my $term = 3 ** -$x;
-    $y = $term-$c; $t = $sum+$y; $c = ($t-$sum)-$y; $sum = $t;
-    $t = 1.0 / (1.0 - (2 ** -$x));
-    $sum *= $t;
-    $sum += ($t - 1.0);
-    return $sum;
-  }
 
-  return Math::BigFloat->new($_Riemann_Zeta_Table[int($x)-2])
-      if $x == int($x) && defined $_Riemann_Zeta_Table[int($x)-2];
-
-  $tol = 1e-40 unless defined $tol;
-
-  # Trying to work around Math::BigFloat bugs RT 43692 and RT 77105 which make
-  # a right mess of things.  Watch this:
-  #   my $n = Math::BigFloat->new(11); $n->accuracy(64); say $n**1.1;  # 13.98
-  #   my $n = Math::BigFloat->new(11); $n->accuracy(67); say $n**1.1;  # 29.98
-  # We can fix some issues with large exponents (e.g. 6^-40.5) by turning it
-  # into (6^-(40.5/4))^4  (assuming the base is positive).  Without that hack,
-  # none of this would work at all.
-
-  $x = Math::BigFloat->new($x);
-  my $superx = 1;
-  my $subx = Math::BigFloat->new($x);
-  while ($subx > 8) {
-    $superx *= 2;
-    $subx /= 2;
-  }
+sub RiemannZeta {
+  my($x, $tol) = @_;
 
-  # Go with the basic formula for large x, as it best works around the mess,
-  # though is unfortunately much slower.
-  if ($x > 30) {
-    my $sum = 0.0;
-    for my $k (4 .. 1000) {
-      my $term = ( $k ** -$subx )  ** $superx;
-      $sum += $term;
-      last if $term < ($sum*$tol);
-    }
-    for my $k (3, 2) {
-      my $term = ( $k ** -$subx )  ** $superx;
-      $sum += $term;
-    }
-    return $sum;
+  if (defined $bignum::VERSION || ref($x) =~ /^Math::Big/) {
+    require Math::Prime::Util::ZetaBigFloat;
+    return Math::Prime::Util::ZetaBigFloat::RiemannZeta($x, $tol);
   }
-  #if ($x > 25) {
-  #  my $sum = 0.0;
-  #  my $divisor = 1.0 - ((2 ** -$subx) ** $superx);
-  #  for my $k (2 .. 1000) {
-  #    my $term = ( (2*$k+1) ** -$subx )  ** $superx;
-  #    $sum += $term;
-  #    last if $term < ($tol*$divisor);
-  #  }
-  #  $sum += (3 ** -$subx) ** $superx;
-  #  my $t = 1.0 / $divisor;
-  #  $sum *= $t;
-  #  $sum += ($t - 1.0);
-  #  return $sum;
-  #}
 
-  # If we wanted to change the Borwein series being used:
-  # _Recompute_Dk(55);
-
-  if (ref $_Borwein_dk[0] ne 'Math::BigInt') {
-    @_Borwein_dk = map { Math::BigInt->new("$_") } @_Borwein_dk;
-  }
+  return 0.0 + $_Riemann_Zeta_Table[int($x)-2]
+    if $x == int($x) && defined $_Riemann_Zeta_Table[int($x)-2];
+  $tol = 1e-16 unless defined $tol;
+  my($y, $t);
+  my $sum = 0.0;
+  my $c = 0.0;
 
-  my $n = $_Borwein_n;
-  my $intermediate_accuracy = undef;
-  my $one = Math::BigFloat->bone;
-  $one->accuracy($intermediate_accuracy) if defined $intermediate_accuracy;
-
-  my $d1 = Math::BigFloat->new(2);
-  $d1->accuracy($intermediate_accuracy) if defined $intermediate_accuracy;
-  # with bignum on, $d1->bpow($one-$x) doesn't change d1 !
-  $d1 = $d1 ** ($one - $x);
-  my $divisor = $one->copy->bsub($d1)->bmul(-$_Borwein_dk[$n]);
-
-  $tol = $divisor->copy->bmul($tol)->babs();
-
-  my $sum = Math::BigFloat->bzero;
-  $sum->accuracy($intermediate_accuracy) if defined $intermediate_accuracy;
-  foreach my $k (1 .. $n-1) {
-    my $term = Math::BigFloat->new( $_Borwein_dk[$k] - $_Borwein_dk[$n] );
-    $term *= -1 if $k % 2;
-    $term->accuracy($intermediate_accuracy) if defined $intermediate_accuracy;
-    my $den = Math::BigFloat->new($k+1);
-    $den->accuracy($intermediate_accuracy) if defined $intermediate_accuracy;
-    $den = ($den ** $subx) ** $superx;
-    $term /= $den;
-    $sum += $term;
-    last if $term->copy->babs() < $tol;
+  for my $k (2 .. 1000000) {
+    my $term = (2*$k+1) ** -$x;
+    $y = $term-$c; $t = $sum+$y; $c = ($t-$sum)-$y; $sum = $t;
+    last if $term < abs($tol*$sum);
   }
-  $sum += Math::BigFloat->new( $one - $_Borwein_dk[$n] ); # term k=0
-  $sum->bdiv( $divisor );
-  $sum->bsub(1);
+  my $term = 3 ** -$x;
+  $y = $term-$c; $t = $sum+$y; $c = ($t-$sum)-$y; $sum = $t;
+  $t = 1.0 / (1.0 - (2 ** -$x));
+  $sum *= $t;
+  $sum += ($t - 1.0);
   return $sum;
 }
 
 # Riemann R function
 sub RiemannR {
   my($x, $tol) = @_;
-  my($y, $t);
 
   croak "Invalid input to ReimannR:  x must be > 0" if $x <= 0;
 
-  if (!defined $bignum::VERSION && ref($x) !~ /^Math::Big/) {
-    $tol = 1e-16 unless defined $tol;
-    my $sum = 0.0;
-    my $c = 0.0;
-
-    $y = 1.0-$c; $t = $sum+$y; $c = ($t-$sum)-$y; $sum = $t;
-    my $flogx = log($x);
-    my $part_term = 1.0;
-    for my $k (1 .. 10000) {
-      # Small k from table, larger k from function
-      my $zeta = ($k <= $#_Riemann_Zeta_Table)
-                 ? $_Riemann_Zeta_Table[$k+1-2]
-                 : RiemannZeta($k+1);
-      $part_term *= $flogx / $k;
-      my $term = $part_term / ($k + $k * $zeta);
-      $y = $term-$c; $t = $sum+$y; $c = ($t-$sum)-$y; $sum = $t;
-      last if abs($term/$sum) < $tol;
-    }
-    return $sum;
+  if (defined $bignum::VERSION || ref($x) =~ /^Math::Big/) {
+    require Math::Prime::Util::ZetaBigFloat;
+    return Math::Prime::Util::ZetaBigFloat::RiemannR($x, $tol);
   }
 
-  $x = new Math::BigFloat "$x"  if ref($x) ne 'Math::BigFloat';
 
-  $tol = 1e-35 unless defined $tol;
-  my $sum = Math::BigFloat->bzero;
-  my $c = Math::BigFloat->bzero;
+  $tol = 1e-16 unless defined $tol;
+  my $sum = 0.0;
+  my($y, $t);
+  my $c = 0.0;
 
   $y = 1.0-$c; $t = $sum+$y; $c = ($t-$sum)-$y; $sum = $t;
   my $flogx = log($x);
-  my $part_term = Math::BigFloat->bone;
-
+  my $part_term = 1.0;
   for my $k (1 .. 10000) {
     # Small k from table, larger k from function
     my $zeta = ($k <= $#_Riemann_Zeta_Table)
-               ? Math::BigFloat->new($_Riemann_Zeta_Table[$k+1-2])
+               ? $_Riemann_Zeta_Table[$k+1-2]
                : RiemannZeta($k+1);
     $part_term *= $flogx / $k;
     my $term = $part_term / ($k + $k * $zeta);
     $y = $term-$c; $t = $sum+$y; $c = ($t-$sum)-$y; $sum = $t;
-    last if abs($term/$sum) < $tol;
+    last if $term < ($tol * $sum);
   }
   return $sum;
 }
diff --git a/lib/Math/Prime/Util/ZetaBigFloat.pm b/lib/Math/Prime/Util/ZetaBigFloat.pm
new file mode 100644
index 0000000..01cd860
--- /dev/null
+++ b/lib/Math/Prime/Util/ZetaBigFloat.pm
@@ -0,0 +1,509 @@
+package Math::Prime::Util::ZetaBigFloat;
+use strict;
+use warnings;
+
+BEGIN {
+  $Math::Prime::Util::ZetaBigFloat::AUTHORITY = 'cpan:DANAJ';
+  $Math::Prime::Util::ZetaBigFloat::VERSION = '0.14';
+}
+
+use Math::BigFloat;
+
+# Riemann Zeta($k) for integer $k.
+# So many terms and digits are used so we can quickly do bignum R.
+my @_Riemann_Zeta_Table = (
+  '0.64493406684822643647241516664602518921894990',   # zeta(2) - 1
+  '0.20205690315959428539973816151144999076498629',
+  '0.082323233711138191516003696541167902774750952',
+  '0.036927755143369926331365486457034168057080920',
+  '0.017343061984449139714517929790920527901817490',
+  '0.0083492773819228268397975498497967595998635606',
+  '0.0040773561979443393786852385086524652589607906',
+  '0.0020083928260822144178527692324120604856058514',
+  '0.00099457512781808533714595890031901700601953156',
+  '0.00049418860411946455870228252646993646860643576',
+  '0.00024608655330804829863799804773967096041608846',
+  '0.00012271334757848914675183652635739571427510590',
+  '0.000061248135058704829258545105135333747481696169',
+  '0.000030588236307020493551728510645062587627948707',
+  '0.000015282259408651871732571487636722023237388990',
+  '0.0000076371976378997622736002935630292130882490903',
+  '0.0000038172932649998398564616446219397304546972190',
+  '0.0000019082127165539389256569577951013532585711448',
+  '0.00000095396203387279611315203868344934594379418741',
+  '0.00000047693298678780646311671960437304596644669478',
+  '0.00000023845050272773299000364818675299493504182178',
+  '0.00000011921992596531107306778871888232638725499778',
+  '0.000000059608189051259479612440207935801227503918837',
+  '0.000000029803503514652280186063705069366011844730920',
+  '0.000000014901554828365041234658506630698628864788168',
+  '0.0000000074507117898354294919810041706041194547190319',
+  '0.0000000037253340247884570548192040184024232328930593',
+  '0.0000000018626597235130490064039099454169480616653305',
+  '0.00000000093132743241966818287176473502121981356795514',
+  '0.00000000046566290650337840729892332512200710626918534',
+  '0.00000000023283118336765054920014559759404950248298228',
+  '0.00000000011641550172700519775929738354563095165224717',
+  '0.000000000058207720879027008892436859891063054173122605',
+  '0.000000000029103850444970996869294252278840464106981987',
+  '0.000000000014551921891041984235929632245318420983808894',
+  '0.0000000000072759598350574810145208690123380592648509256',
+  '0.0000000000036379795473786511902372363558732735126460284',
+  '0.0000000000018189896503070659475848321007300850305893096',
+  '0.00000000000090949478402638892825331183869490875386000099',
+  '0.00000000000045474737830421540267991120294885703390452991',
+  '0.00000000000022737368458246525152268215779786912138298220',
+  '0.00000000000011368684076802278493491048380259064374359028',
+  '0.000000000000056843419876275856092771829675240685530571589',
+  '0.000000000000028421709768893018554550737049426620743688265',
+  '0.000000000000014210854828031606769834307141739537678698606',
+  '0.0000000000000071054273952108527128773544799568000227420436',
+  '0.0000000000000035527136913371136732984695340593429921456555',
+  '0.0000000000000017763568435791203274733490144002795701555086',
+  '0.00000000000000088817842109308159030960913863913863256088715',
+  '0.00000000000000044408921031438133641977709402681213364596031',
+  '0.00000000000000022204460507980419839993200942046539642366543',
+  '0.00000000000000011102230251410661337205445699213827024832229',
+  '0.000000000000000055511151248454812437237365905094302816723551',
+  '0.000000000000000027755575621361241725816324538540697689848904',
+  '0.000000000000000013877787809725232762839094906500221907718625',
+  '0.0000000000000000069388939045441536974460853262498092748358742',
+  '0.0000000000000000034694469521659226247442714961093346219504706',
+  '0.0000000000000000017347234760475765720489729699375959074780545',
+  '0.00000000000000000086736173801199337283420550673429514879071415',
+  '0.00000000000000000043368086900206504874970235659062413612547801',
+  '0.00000000000000000021684043449972197850139101683209845761574010',
+  '0.00000000000000000010842021724942414063012711165461382589364744',
+  '0.000000000000000000054210108624566454109187004043886337150634224',
+  '0.000000000000000000027105054312234688319546213119497764318887282',
+  '0.000000000000000000013552527156101164581485233996826928328981877',
+  '0.0000000000000000000067762635780451890979952987415566862059812586',
+  '0.0000000000000000000033881317890207968180857031004508368340311585',
+  '0.0000000000000000000016940658945097991654064927471248619403036418',
+  '0.00000000000000000000084703294725469983482469926091821675222838642',
+  '0.00000000000000000000042351647362728333478622704833579344088109717',
+  '0.00000000000000000000021175823681361947318442094398180025869417612',
+  '0.00000000000000000000010587911840680233852265001539238398470699902',
+  '0.000000000000000000000052939559203398703238139123029185055866375629',
+  '0.000000000000000000000026469779601698529611341166842038715592556134',
+  '0.000000000000000000000013234889800848990803094510250944989684323826',
+  '0.0000000000000000000000066174449004244040673552453323082200147137975',
+  '0.0000000000000000000000033087224502121715889469563843144048092764894',
+  '0.0000000000000000000000016543612251060756462299236771810488297723589',
+  '0.00000000000000000000000082718061255303444036711056167440724040096811',
+  '0.00000000000000000000000041359030627651609260093824555081412852575873',
+  '0.00000000000000000000000020679515313825767043959679193468950443365312',
+  '0.00000000000000000000000010339757656912870993284095591745860911079606',
+  '0.000000000000000000000000051698788284564313204101332166355512893608164',
+  '0.000000000000000000000000025849394142282142681277617708450222269121159',
+  '0.000000000000000000000000012924697071141066700381126118331865309299779',
+  '0.0000000000000000000000000064623485355705318034380021611221670660356864',
+  '0.0000000000000000000000000032311742677852653861348141180266574173608296',
+  '0.0000000000000000000000000016155871338926325212060114057052272720509148',
+  '0.00000000000000000000000000080779356694631620331587381863408997398684847',
+  '0.00000000000000000000000000040389678347315808256222628129858130379479700',
+  '0.00000000000000000000000000020194839173657903491587626465673047518903728',
+  '0.00000000000000000000000000010097419586828951533619250700091044144538432',
+  '0.000000000000000000000000000050487097934144756960847711725486604360898735',
+  '0.000000000000000000000000000025243548967072378244674341937966175648398693',
+  '0.000000000000000000000000000012621774483536189043753999660777148710632765',
+  '0.0000000000000000000000000000063108872417680944956826093943332037500694712',
+  '0.0000000000000000000000000000031554436208840472391098412184847972814371270',
+  '0.0000000000000000000000000000015777218104420236166444327830159601782237092',
+  '0.00000000000000000000000000000078886090522101180735205378276604136878962534',
+  '0.00000000000000000000000000000039443045261050590335263935513575963608141044',
+  '0.00000000000000000000000000000019721522630525295156852383215213909988473843',
+  '0.000000000000000000000000000000098607613152626475748329967604159218377505181',
+  '0.000000000000000000000000000000049303806576313237862187667644776975622245754',
+  '0.000000000000000000000000000000024651903288156618927101395103287812527732549',
+  '0.000000000000000000000000000000012325951644078309462219884645277065145764150',
+  '0.0000000000000000000000000000000061629758220391547306663380205162648609383631',
+  '0.0000000000000000000000000000000030814879110195773651853009095507130250105264',
+  '0.0000000000000000000000000000000015407439555097886825433610878728841686496904',
+  '0.00000000000000000000000000000000077037197775489434125525075496895150086398231',
+  '0.00000000000000000000000000000000038518598887744717062214878116197893873445220',
+  '0.00000000000000000000000000000000019259299443872358530924885847349054449873362',
+  '0.000000000000000000000000000000000096296497219361792654015918534245633717541108',
+  '0.000000000000000000000000000000000048148248609680896326805122366289604787579935',
+  '0.000000000000000000000000000000000024074124304840448163334948882867065229914248',
+  '0.000000000000000000000000000000000012037062152420224081644937008007620275295506',
+  '0.0000000000000000000000000000000000060185310762101120408149560261951727031681191',
+  '0.0000000000000000000000000000000000030092655381050560204049738538280405431094080',
+  '0.0000000000000000000000000000000000015046327690525280102016522071575050028177934',
+  '0.00000000000000000000000000000000000075231638452626400510054786365991407868525313',
+  '0.00000000000000000000000000000000000037615819226313200255018118519034423181524371',
+  '0.00000000000000000000000000000000000018807909613156600127505967704863451341028548',
+  '0.000000000000000000000000000000000000094039548065783000637519533342138055875645097',
+  '0.000000000000000000000000000000000000047019774032891500318756331610342627662060287',
+  '0.000000000000000000000000000000000000023509887016445750159377020784929180405960294',
+  '0.000000000000000000000000000000000000011754943508222875079688128719050545728002924',
+  '0.0000000000000000000000000000000000000058774717541114375398439371350539247056872356',
+  '0.0000000000000000000000000000000000000029387358770557187699219261593698463000750878',
+  '0.0000000000000000000000000000000000000014693679385278593849609489436325511324487536',
+  '0.00000000000000000000000000000000000000073468396926392969248046975979881822702829326',
+  '0.00000000000000000000000000000000000000036734198463196484624023330922692333378216377',
+  '0.00000000000000000000000000000000000000018367099231598242312011613105596640698043218',
+  '0.000000000000000000000000000000000000000091835496157991211560057891008818116853335663',
+  '0.000000000000000000000000000000000000000045917748078995605780028887331354029547708393',
+  '0.000000000000000000000000000000000000000022958874039497802890014424274658671814201226',
+  '0.000000000000000000000000000000000000000011479437019748901445007205673656554920549667',
+  '0.0000000000000000000000000000000000000000057397185098744507225036006822706837980911955',
+  '0.0000000000000000000000000000000000000000028698592549372253612517996229494773449843879',
+  '0.0000000000000000000000000000000000000000014349296274686126806258995720794504878051247',
+  '0.00000000000000000000000000000000000000000071746481373430634031294970624129584900687276',
+  '0.00000000000000000000000000000000000000000035873240686715317015647482652117145953820656',
+  '0.00000000000000000000000000000000000000000017936620343357658507823740439409357478069335',
+  '0.000000000000000000000000000000000000000000089683101716788292539118699241549402394210037',
+  '0.000000000000000000000000000000000000000000044841550858394146269559348635608906198392806',
+  '0.000000000000000000000000000000000000000000022420775429197073134779673989415854766292332',
+  '0.000000000000000000000000000000000000000000011210387714598536567389836885245061272178142',
+  '0.0000000000000000000000000000000000000000000056051938572992682836949184061349085990997301',
+  '0.0000000000000000000000000000000000000000000028025969286496341418474591909049136205534180',
+  '0.0000000000000000000000000000000000000000000014012984643248170709237295913982765839445600',
+  '0.00000000000000000000000000000000000000000000070064923216240853546186479434774488319489698',
+  '0.00000000000000000000000000000000000000000000035032461608120426773093239672340797200498749',
+  '0.00000000000000000000000000000000000000000000017516230804060213386546619821154916280500674',
+  '0.000000000000000000000000000000000000000000000087581154020301066932733099055722973670007705',
+  '0.000000000000000000000000000000000000000000000043790577010150533466366549511177617590838630',
+  '0.000000000000000000000000000000000000000000000021895288505075266733183274750027519047364241',
+  '0.000000000000000000000000000000000000000000000010947644252537633366591637373159996274330429',
+  '0.0000000000000000000000000000000000000000000000054738221262688166832958186859620770540479841',
+  '0.0000000000000000000000000000000000000000000000027369110631344083416479093427750648326515819',
+  '0.0000000000000000000000000000000000000000000000013684555315672041708239546713188745182016542',
+  '0.00000000000000000000000000000000000000000000000068422776578360208541197733563655129305944821',
+  '0.00000000000000000000000000000000000000000000000034211388289180104270598866781064699118259780',
+  '0.00000000000000000000000000000000000000000000000017105694144590052135299433390278061047559013',
+  '0.000000000000000000000000000000000000000000000000085528470722950260676497166950542676865892145',
+  '0.000000000000000000000000000000000000000000000000042764235361475130338248583474988795642311765',
+  '0.000000000000000000000000000000000000000000000000021382117680737565169124291737400216890944447',
+  '0.000000000000000000000000000000000000000000000000010691058840368782584562145868668714802068411',
+  '0.0000000000000000000000000000000000000000000000000053455294201843912922810729343238928532329351',
+  '0.0000000000000000000000000000000000000000000000000026727647100921956461405364671584582440160440',
+  '0.0000000000000000000000000000000000000000000000000013363823550460978230702682335780663944745475',
+  '0.00000000000000000000000000000000000000000000000000066819117752304891153513411678864562139278223',
+  '0.00000000000000000000000000000000000000000000000000033409558876152445576756705839419361874822728',
+  '0.00000000000000000000000000000000000000000000000000016704779438076222788378352919705374539139236',
+  '0.000000000000000000000000000000000000000000000000000083523897190381113941891764598512518034789088',
+  '0.000000000000000000000000000000000000000000000000000041761948595190556970945882299251474130425513',
+  '0.000000000000000000000000000000000000000000000000000020880974297595278485472941149624142102889746',
+  '0.000000000000000000000000000000000000000000000000000010440487148797639242736470574811539397337203',
+  '0.0000000000000000000000000000000000000000000000000000052202435743988196213682352874055924806327115',
+  '0.0000000000000000000000000000000000000000000000000000026101217871994098106841176437027371676377257',
+  '0.0000000000000000000000000000000000000000000000000000013050608935997049053420588218513488929259862',
+  '0.00000000000000000000000000000000000000000000000000000065253044679985245267102941092566788283203421',
+);
+# Convert to BigFloat objects.
+ at _Riemann_Zeta_Table = map { Math::BigFloat->new($_) } @_Riemann_Zeta_Table;
+# for k = 1 .. n :  (1 / (zeta(k+1) * k + k)
+# Makes RiemannR run about twice as fast.
+my @_Riemann_Zeta_Premult;
+
+# Select n = 55, good for 46ish digits of accuracy.
+my $_Borwein_n = 55;
+my @_Borwein_dk = (
+  '1',
+  '6051',
+  '6104451',
+  '2462539971',
+  '531648934851',
+  '71301509476803',
+  '6504925195108803',
+  '429144511928164803',
+  '21392068013887742403',
+  '832780518854440804803',
+  '25977281563850106233283',
+  '662753606729324750201283',
+  '14062742362385399866745283',
+  '251634235316509414702211523',
+  '3841603462178827861104812483',
+  '50535961819850087101900022211',
+  '577730330374203014014104003011',
+  '5782012706584553297863989289411',
+  '50984922488525881477588707205571',
+  '398333597655022403279683908035011',
+  '2770992240330783259897072664469955',
+  '17238422988353715312442126057365955',
+  '96274027751337344115352100618133955',
+  '484350301573059857715727453968687555',
+  '2201794236784087151947175826243477955',
+  '9068765987529892610841571032285864387',
+  '33926582279822401059328069515697217987',
+  '115535262182820447663793177744255246787',
+  '358877507711760077538925500462137369027',
+  '1018683886695854101193095537014797787587',
+  '2646951832121008166346437186541363159491',
+  '6306464665572570713623910486640730071491',
+  '13799752848354341643763498672558481367491',
+  '27780237373991939435100856211039992177091',
+  '51543378762608611361377523633779417047491',
+  '88324588911945720951614452340280439890371',
+  '140129110249040241501243929391690331218371',
+  '206452706984942815385219764876242498642371',
+  '283527707823296964404071683165658912154051',
+  '364683602811933600833512164561308162744771',
+  '441935796522635816776473230396154031661507',
+  '508231717051242054487234759342047053767107',
+  '559351463001010719709990637083458540691907',
+  '594624787018881191308291683229515933311427',
+  '616297424973434835299724300924272199623107',
+  '628083443816135918099559567176252011864515',
+  '633714604276098212796088600263676671320515',
+  '636056734158553360761837806887547188568515',
+  '636894970116484676875895417679248215794115',
+  '637149280289288581322870186196318041432515',
+  '637213397278310656625865036925470191411651',
+  '637226467136294189739463288384528579584451',
+  '637228536449134002301138291602841035366851',
+  '637228775173095037281299181461988671775171',
+  '637228793021615488494769154535569803469251',
+  '637228793670652595811622608101881844621763',
+);
+# "An Efficient Algorithm for the Riemann Zeta Function", Borwein, 1991.
+# About 1.3n terms are needed for n digits of accuracy.
+sub _Recompute_Dk {
+  my $nterms = shift;
+  $_Borwein_n = $nterms;
+  @_Borwein_dk = ();
+  foreach my $k (0 .. $nterms) {
+    my $dsum = Math::BigFloat->bzero;
+    $dsum->accuracy(2*$_Borwein_n);
+    my $n = Math::BigInt->new($nterms-1)->bfac;
+    my $d = Math::BigInt->new($nterms)->bfac;
+    foreach my $i (0 .. $k) {
+      my $term = Math::BigFloat->bone;
+      $term->accuracy(2*$_Borwein_n);
+      $term->bmul($n)->bdiv($d);
+      $dsum += $term;
+      $n->bmul($nterms+$i)->bmul(4);
+      $d->bdiv($nterms-$i)->bmul(2*$i+1)->bmul(2*$i+2);
+    }
+    my $dk = ($nterms * $dsum + 1e-20)->as_int;
+    $_Borwein_dk[$k] = $dk;
+    #print  "  '$dk',\n";
+  }
+}
+
+sub RiemannZeta {
+  my($x, $tol) = @_;
+
+  return $_Riemann_Zeta_Table[int($x)-2]
+      if $x == int($x) && defined $_Riemann_Zeta_Table[int($x)-2];
+
+  $tol = 1e-40 unless defined $tol;
+
+  # Trying to work around Math::BigFloat bugs RT 43692 and RT 77105 which make
+  # a right mess of things.  Watch this:
+  #   my $n = Math::BigFloat->new(11); $n->accuracy(64); say $n**1.1;  # 13.98
+  #   my $n = Math::BigFloat->new(11); $n->accuracy(67); say $n**1.1;  # 29.98
+  # We can fix some issues with large exponents (e.g. 6^-40.5) by turning it
+  # into (6^-(40.5/4))^4  (assuming the base is positive).  Without that hack,
+  # none of this would work at all.
+
+  $x = Math::BigFloat->new($x);
+  my $superx = 1;
+  my $subx = Math::BigFloat->new($x);
+  while ($subx > 8) {
+    $superx *= 2;
+    $subx /= 2;
+  }
+
+  # Go with the basic formula for large x, as it best works around the mess,
+  # though is unfortunately much slower.
+  if ($x > 30) {
+    my $sum = 0.0;
+    for my $k (4 .. 1000) {
+      my $term = ( $k ** -$subx )  ** $superx;
+      $sum += $term;
+      last if $term < ($sum*$tol);
+    }
+    $sum += ( 3 ** -$subx )  ** $superx;
+    $sum += ( 2 ** -$subx )  ** $superx;
+    return $sum;
+  }
+  #if ($x > 25) {
+  #  my $sum = 0.0;
+  #  my $divisor = 1.0 - ((2 ** -$subx) ** $superx);
+  #  for my $k (2 .. 1000) {
+  #    my $term = ( (2*$k+1) ** -$subx )  ** $superx;
+  #    $sum += $term;
+  #    last if $term < ($tol*$divisor);
+  #  }
+  #  $sum += (3 ** -$subx) ** $superx;
+  #  my $t = 1.0 / $divisor;
+  #  $sum *= $t;
+  #  $sum += ($t - 1.0);
+  #  return $sum;
+  #}
+
+  # If we wanted to change the Borwein series being used:
+  # _Recompute_Dk(55);
+
+  if (ref $_Borwein_dk[0] ne 'Math::BigInt') {
+    @_Borwein_dk = map { Math::BigInt->new("$_") } @_Borwein_dk;
+  }
+
+  my $n = $_Borwein_n;
+  my $intermediate_accuracy = undef;
+  my $one = Math::BigFloat->bone;
+  $one->accuracy($intermediate_accuracy) if defined $intermediate_accuracy;
+
+  my $d1 = Math::BigFloat->new(2);
+  $d1->accuracy($intermediate_accuracy) if defined $intermediate_accuracy;
+  # with bignum on, $d1->bpow($one-$x) doesn't change d1 !
+  $d1 = $d1 ** ($one - $x);
+  my $divisor = $one->copy->bsub($d1)->bmul(-$_Borwein_dk[$n]);
+
+  $tol = $divisor->copy->bmul($tol)->babs();
+
+  my $sum = Math::BigFloat->bzero;
+  $sum->accuracy($intermediate_accuracy) if defined $intermediate_accuracy;
+  foreach my $k (1 .. $n-1) {
+    my $term = Math::BigFloat->new( $_Borwein_dk[$k] - $_Borwein_dk[$n] );
+    $term *= -1 if $k % 2;
+    $term->accuracy($intermediate_accuracy) if defined $intermediate_accuracy;
+    my $den = Math::BigFloat->new($k+1);
+    $den->accuracy($intermediate_accuracy) if defined $intermediate_accuracy;
+    $den = ($den ** $subx) ** $superx;
+    $term /= $den;
+    $sum += $term;
+    last if $term->copy->babs() < $tol;
+  }
+  $sum += Math::BigFloat->new( $one - $_Borwein_dk[$n] ); # term k=0
+  $sum->bdiv( $divisor );
+  $sum->bsub(1);
+  return $sum;
+}
+
+# Riemann R function
+sub RiemannR {
+  my($x, $tol) = @_;
+
+  if (scalar @_Riemann_Zeta_Premult == 0) {
+    @_Riemann_Zeta_Premult = map { my $v = Math::BigFloat->bone;
+                                   $v->accuracy($x->accuracy() || 45);
+                                   $v / ($_Riemann_Zeta_Table[$_-1] * $_ + $_) }
+                             (1 .. @_Riemann_Zeta_Table);
+  }
+
+  $x = new Math::BigFloat "$x"  if ref($x) ne 'Math::BigFloat';
+
+  $tol = 1e-35 unless defined $tol;
+  my $sum = Math::BigFloat->bone;
+
+  my $flogx = log($x);
+  my $part_term = Math::BigFloat->bone;
+
+  for my $k (1 .. 10000) {
+    my $zeta_term = $_Riemann_Zeta_Premult[$k-1];
+    if (!defined $zeta_term) {
+      my $zeta = (($k-1) <= $#_Riemann_Zeta_Table)
+                 ? $_Riemann_Zeta_Table[$k-1]
+                 : RiemannZeta( $k+1 );
+      $zeta_term = Math::BigFloat->bone / ($zeta * $k + $k);
+    }
+    $part_term *= $flogx / $k;
+    my $term = $part_term * $zeta_term;
+    #warn "k = $k  term = $term\n";
+    $sum += $term;
+    last if $term < ($tol*$sum);
+  }
+  return $sum;
+}
+
+1;
+
+__END__
+
+
+# ABSTRACT: Perl Big Float versions of Riemann Zeta and R functions
+
+=pod
+
+=encoding utf8
+
+
+=head1 NAME
+
+Math::Prime::Util::ZetaBigFloat - Perl Big Float versions of Riemann Zeta and R functions
+
+
+=head1 VERSION
+
+Version 0.14
+
+
+=head1 SYNOPSIS
+
+Math::BigFloat versions`of the Riemann Zeta and Riemann R functions.  These
+are kept in a separate module because they use a lot of big tables that we'd
+prefer not to have loaded all the time.
+
+
+=head1 DESCRIPTION
+
+Pure Perl implementations of Riemann Zeta and Riemann R using Math::BigFloat.
+
+
+=head1 FUNCTIONS
+
+=head2 RiemannZeta
+
+  my $z = RiemannZeta($s);
+
+Given a floating point input C<s> where C<s E<gt>= 0.5>, returns the floating
+point value of ζ(s)-1, where ζ(s) is the Riemann zeta function.  One is
+subtracted to ensure maximum precision for large values of C<s>.  The zeta
+function is the sum from k=1 to infinity of C<1 / k^s>
+
+Accuracy should be at least 14 digits, but currently does not increase
+accuracy with big floats.  Small integer values are returned from a table,
+values between 0.5 and 5 use rational Chebyshev approximation, and larger
+values use a series.
+
+
+=head2 RiemannR
+
+  my $r = RiemannR($x);
+
+Given a positive non-zero floating point input, returns the floating
+point value of Riemann's R function.  Riemann's R function gives a very close
+approximation to the prime counting function.
+
+Accuracy should be at least 14 digits.
+
+
+=head1 LIMITATIONS
+
+Bugs in Math::BigFloat (RT 43692, RT 77105) cause many problems with this code.
+I've attempted to apply workarounds, but it is possible there are cases they
+miss.
+
+The accuracy goals (35 digits) are sometimes missed by a digit or two, and
+extensive testing needs to be done to ensure we meet the goals.
+
+
+=head1 PERFORMANCE
+
+Performance is not good at all.  A version using XS+GMP would be good to have.
+Pari can give better accuracy in a miniscule fraction of the time.
+
+
+=head1 SEE ALSO
+
+L<Math::Prime::Util>
+
+L<Math::Pari>
+
+
+=head1 AUTHORS
+
+Dana Jacobsen E<lt>dana at acm.orgE<gt>
+
+
+=head1 COPYRIGHT
+
+Copyright 2012 by Dana Jacobsen E<lt>dana at acm.orgE<gt>
+
+This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
+
+=cut
diff --git a/t/14-nthprime.t b/t/14-nthprime.t
index c4d3a9d..28abc61 100644
--- a/t/14-nthprime.t
+++ b/t/14-nthprime.t
@@ -47,15 +47,13 @@ my %nthprimes64 = (
  100000000000000000 => 4185296581467695669,
 );
 my %nthprimes_small = map { $_ => $nthprimes32{$_} }
-                      #grep { ($_ <= 2000000) || $extra }
+                      #grep { ($_ <= 2_000_000) || $extra }
                       keys %nthprimes32;
 
 my @small_primes = (0, @{primes($nth_small_prime)});
 
-#plan tests => 7*2 + $nsmallprimes+1 + 9*3 + 7 + ($extra ? 9 : 7) + ($use64 ? 9*3 : 0);
-
 plan tests => 0 + 2*scalar(keys %pivals32)
-                + scalar @small_primes
+                + 1
                 + 3*scalar(keys %nthprimes32)
                 + scalar(keys %nthprimes_small)
                 + $use64 * 3 * scalar(keys %nthprimes64)
@@ -69,8 +67,9 @@ while (my($n, $pin) = each (%pivals32)) {
   cmp_ok( nth_prime($next), '>=', $n, "nth_prime($next) >= $n");
 }
 
-foreach my $n (0 .. $#small_primes) {
-  is(nth_prime($n), $small_primes[$n], "The ${n}th prime is $small_primes[$n]");
+{
+  my @nth_primes = map { nth_prime($_) }  (0 .. $#small_primes);
+  is_deeply( \@nth_primes, \@small_primes, "nth_prime for primes 0 .. $#small_primes" );
 }
 
 while (my($n, $nth) = each (%nthprimes32)) {
diff --git a/t/15-probprime.t b/t/15-probprime.t
index 228a022..b622726 100644
--- a/t/15-probprime.t
+++ b/t/15-probprime.t
@@ -3,110 +3,114 @@ use strict;
 use warnings;
 
 use Test::More;
-use Math::Prime::Util qw/is_prime is_prob_prime/;
+use Math::Prime::Util qw/is_prob_prime/;
 
 my $use64 = Math::Prime::Util::prime_get_config->{'maxbits'} > 32;
 my $extra = defined $ENV{RELEASE_TESTING} && $ENV{RELEASE_TESTING};
-
-plan tests => 6 + 19 + 3573 + (5 + 29 + 22 + 23 + 16) + 15 + 27
-              + ($use64 ? 5+1 : 0)
-              + ($extra ? 6 : 0)
-              + (($extra && $use64) ? 19 : 0);
-
-# Some of these tests were inspired by Math::Primality's tests
-
-ok(is_prob_prime(2)  == 2,  '2 is prime');
-ok(is_prob_prime(1)  == 0,  '1 is not prime');
-ok(is_prob_prime(0)  == 0,  '0 is not prime');
-ok(is_prob_prime(-1) == 0, '-1 is not prime');
-ok(is_prob_prime(-2) == 0, '-2 is not prime');
-ok(is_prob_prime(20) == 0, '20 is not prime');
-
-# powers of 2
-foreach my $k (2 .. 20) {
-  my $k2 = 2**$k;
-  ok(!is_prob_prime($k2), "2**$k=$k2 is not prime");
-}
+my $broken64 = (18446744073709550592 == ~0);
 
 my @small_primes = qw/
-2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
-73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173
-179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281
-283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
-419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541
-547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659
-661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809
-811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941
-947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069
-1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223
-1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373
-1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511
-1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657
-1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811
-1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987
-1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129
-2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287
-2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423
-2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 2609 2617
-2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741
-2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903
-2909 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079
-3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257
-3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413
-3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571
-/;
-my %small_primes;
-map { $small_primes{$_} = 1; } @small_primes;
-
-foreach my $n (0 .. 3572) {
-  if (defined $small_primes{$n}) {
-    ok(is_prob_prime($n)==2, "$n is prime");
-  } else {
-    ok(!is_prob_prime($n), "$n is not prime");
-  }
+2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
+101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197
+199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313
+317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439
+443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571
+577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691
+701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829
+839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977
+983 991 997
+1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097
+1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223
+1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321
+1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459
+1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571
+1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693
+1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811
+1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949
+1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069
+2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203
+2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311
+2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423
+2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579
+2591 2593 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693
+2699 2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801
+2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939
+2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079
+3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221
+3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347
+3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491
+3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 /;
+
+my @composites = (qw/
+  9 121 341 561 645 703 781 1105 1387 1541 1729 1891 1905 2047 2465 2701 2821
+  3277 3281 4033 4369 4371 4681 5461 5611 6601 7813 7957 8321 8401 8911 10585
+  12403 13021 14981 15751 15841 16531 18721 19345 23521 24211 25351 29341
+  29539 31621 38081 40501 41041 44287 44801 46657 47197 52633 53971 55969
+  62745 63139 63973 74593 75361 79003 79381 82513 87913 88357 88573 97567
+  101101 340561 488881 852841 1373653 1857241 6733693 9439201 17236801
+  23382529 25326001 34657141 56052361 146843929 216821881 3215031751 /);
+
+push @composites, (qw/
+  2152302898747 3474749660383 341550071728321 341550071728321
+  3825123056546413051/) if $use64;
+
+my @primes = (qw/
+  2 3 7 23 89 113 523 887 1129 1327 9551 15683 19609 31397 155921
+  5 11 29 97 127 541 907 1151 1361 9587 15727 19661 31469 156007 360749
+  370373 492227 1349651 1357333 2010881 4652507 17051887 20831533 47326913
+  122164969 189695893 191913031 387096383 436273291 1294268779 1453168433
+  2300942869 3842611109/);
+
+push @primes, (qw/
+  4302407713 10726905041 20678048681 22367085353 25056082543 42652618807
+  127976334671 182226896239 241160624143 297501075799 303371455241
+  304599508537 416608695821 461690510011 614487453523 738832927927
+  1346294310749 1408695493609 1968188556461 2614941710599/) if $use64;
+
+# We're checking every integer from 0 to small_primes[-1], so don't bother
+# checking them twice.
+ at composites = grep { $_ > $small_primes[-1] } @composites;
+ at primes     = grep { $_ > $small_primes[-1] } @primes;
+
+
+plan tests =>   6   # range
+              + 1   # powers of 2
+              + 1   # small numbers
+              + scalar @composites
+              + scalar @primes
+              + 1 # 32-bit or 64-bit edge
+              + 0;
+
+ok(!eval { is_prob_prime(undef); }, "is_prob_prime(undef)");
+ok( is_prob_prime(2),  '2 is prime');
+ok(!is_prob_prime(1),  '1 is not prime');
+ok(!is_prob_prime(0),  '0 is not prime');
+ok(!is_prob_prime(-1), '-1 is not prime');
+ok(!is_prob_prime(-2), '-2 is not prime');
+
+{
+  my @isprime = map { 0+!!is_prob_prime( int(2**$_) ) } (2..20);
+  my @exprime = (0) x (20-2+1);
+  is_deeply( \@isprime, \@exprime, "is_prob_prime powers of 2" );
 }
 
-map { ok(!is_prob_prime($_), "A006945 number $_ is not prime") }
-  qw/9 2047 1373653 25326001 3215031751/;
-
-map { ok(!is_prob_prime($_), "A006945 number $_ is not prime") }
-  qw/2152302898747 3474749660383 341550071728321 341550071728321 3825123056546413051/ if $use64;
-
-map { ok(!is_prob_prime($_), "Carmichael Number $_ is not prime") }
-  qw/561 1105 1729 2465 2821 6601 8911 10585 15841 29341 41041 46657 52633
-     62745 63973 75361 101101 340561 488881 852841 1857241 6733693
-     9439201 17236801 23382529 34657141 56052361 146843929 216821881/;
-
-map { ok(!is_prob_prime($_), "Pseudoprime (base 2) $_ is not prime" ) }
-  qw/341 561 645 1105 1387 1729 1905 2047 2465 2701 2821 3277 4033 4369 4371
-     4681 5461 6601 7957 8321 52633 88357/;
-
-map { ok(!is_prob_prime($_), "Pseudoprime (base 3) $_ is not prime" ) }
-  qw/121 703 1891 3281 8401 8911 10585 12403 16531 18721 19345 23521 31621
-     44287 47197 55969 63139 74593 79003 82513 87913 88573 97567/;
-
-map { ok(!is_prob_prime($_), "Pseudoprime (base 5) $_ is not prime" ) }
-  qw/781 1541 5461 5611 7813 13021 14981 15751 24211 25351 29539 38081
-     40501 44801 53971 79381/;
-
-map { ok(is_prob_prime($_)==2, "Primegap start $_ is prime" ) }
-  qw/2 3 7 23 89 113 523 887 1129 1327 9551 15683 19609 31397 155921/;
-
-map { ok(is_prob_prime($_)==2, "Primegap end $_ is prime" ) }
-  qw/5 11 29 97 127 541 907 1151 1361 9587 15727 19661 31469 156007 360749
-     370373 492227 1349651 1357333 2010881 4652507 17051887 20831533 47326913
-     122164969 189695893 191913031/;
-
-map { ok(is_prob_prime($_)==2, "Primegap end $_ is prime" ) }
-  qw/10726905041/ if $use64;
+{
+  my %small_primes = map { $_ => 1; } @small_primes;
+  my @isprime = map { is_prob_prime($_) } (0..3572);
+  my @exprime = map { $small_primes{$_} ? 2 : 0 } (0..3572);
+  is_deeply( \@isprime, \@exprime, "is_prob_prime 0..3572" );
+}
 
-map { ok(is_prob_prime($_)==2, "Primegap end $_ is prime" ) }
-  qw/387096383 436273291 1294268779 1453168433 2300942869 3842611109/ if $extra;
+foreach my $n (@composites) {
+  is( is_prob_prime($n), 0, "$n is composite" );
+}
+foreach my $n (@primes) {
+  is( is_prob_prime($n), 2, "$n is definitely prime" );
+}
 
-map { ok(is_prob_prime($_)==2, "Primegap end $_ is prime" ) }
-  qw/4302407713 20678048681 22367085353
-     25056082543 42652618807 127976334671 182226896239 241160624143
-     297501075799 303371455241 304599508537 416608695821 461690510011
-     614487453523 738832927927 1346294310749 1408695493609 1968188556461
-     2614941710599/
-  if $use64 && $extra;
+# Check that we do the right thing near the word-size edge
+SKIP: {
+  skip "Skipping 64-bit edge case on broken 64-bit Perl", 1 if $use64 && $broken64;
+  eval { is_prob_prime( $use64 ? "18446744073709551629" : "4294967306" ); };
+  like($@, qr/range/i, "is_prob_prime on ~0 + delta without bigint should croak");
+}
diff --git a/t/51-primearray.t b/t/51-primearray.t
index bacb2f8..26ea49d 100644
--- a/t/51-primearray.t
+++ b/t/51-primearray.t
@@ -17,32 +17,36 @@ sub shuffle (@) {
 }
 
 my @small_primes = qw/
-2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
-73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173
-179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281
-283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
-419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541
-547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659
-661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809
-811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941
-947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069
-1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223
-1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373
-1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499 1511
-1523 1531 1543 1549 1553 1559 1567 1571 1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657
-1663 1667 1669 1693 1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811
-1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949 1951 1973 1979 1987
-1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069 2081 2083 2087 2089 2099 2111 2113 2129
-2131 2137 2141 2143 2153 2161 2179 2203 2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287
-2293 2297 2309 2311 2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423
-2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579 2591 2593 2609 2617
-2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693 2699 2707 2711 2713 2719 2729 2731 2741
-2749 2753 2767 2777 2789 2791 2797 2801 2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903
-2909 2917 2927 2939 2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079
-3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221 3229 3251 3253 3257
-3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347 3359 3361 3371 3373 3389 3391 3407 3413
-3433 3449 3457 3461 3463 3467 3469 3491 3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571
-/;
+2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
+101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197
+199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313
+317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439
+443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571
+577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691
+701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829
+839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977
+983 991 997
+1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097
+1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223
+1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321
+1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459
+1471 1481 1483 1487 1489 1493 1499 1511 1523 1531 1543 1549 1553 1559 1567 1571
+1579 1583 1597 1601 1607 1609 1613 1619 1621 1627 1637 1657 1663 1667 1669 1693
+1697 1699 1709 1721 1723 1733 1741 1747 1753 1759 1777 1783 1787 1789 1801 1811
+1823 1831 1847 1861 1867 1871 1873 1877 1879 1889 1901 1907 1913 1931 1933 1949
+1951 1973 1979 1987 1993 1997 1999 2003 2011 2017 2027 2029 2039 2053 2063 2069
+2081 2083 2087 2089 2099 2111 2113 2129 2131 2137 2141 2143 2153 2161 2179 2203
+2207 2213 2221 2237 2239 2243 2251 2267 2269 2273 2281 2287 2293 2297 2309 2311
+2333 2339 2341 2347 2351 2357 2371 2377 2381 2383 2389 2393 2399 2411 2417 2423
+2437 2441 2447 2459 2467 2473 2477 2503 2521 2531 2539 2543 2549 2551 2557 2579
+2591 2593 2609 2617 2621 2633 2647 2657 2659 2663 2671 2677 2683 2687 2689 2693
+2699 2707 2711 2713 2719 2729 2731 2741 2749 2753 2767 2777 2789 2791 2797 2801
+2803 2819 2833 2837 2843 2851 2857 2861 2879 2887 2897 2903 2909 2917 2927 2939
+2953 2957 2963 2969 2971 2999 3001 3011 3019 3023 3037 3041 3049 3061 3067 3079
+3083 3089 3109 3119 3121 3137 3163 3167 3169 3181 3187 3191 3203 3209 3217 3221
+3229 3251 3253 3257 3259 3271 3299 3301 3307 3313 3319 3323 3329 3331 3343 3347
+3359 3361 3371 3373 3389 3391 3407 3413 3433 3449 3457 3461 3463 3467 3469 3491
+3499 3511 3517 3527 3529 3533 3539 3541 3547 3557 3559 3571 /;
 
 my %test_indices = (
      377 => 2593,
@@ -56,23 +60,29 @@ my %test_indices = (
 );
   
 
-plan tests => (3*scalar @small_primes) + 2 + scalar(keys %test_indices) + 8;
+plan tests => 3 + 2 + scalar(keys %test_indices) + 8;
 
 {
   my @primes;  tie @primes, 'Math::Prime::Util::PrimeArray';
+  my (@order, @got, @exp);
 
   # Random
-  my @order = shuffle (0 .. $#small_primes);
-  foreach my $index (@order) {
-    is($primes[$index], $small_primes[$index], "primes[$index] == $small_primes[$index]");
-  }
+  @order = shuffle (0 .. $#small_primes);
+  @got = map { $primes[$_] } @order;
+  @exp = map { $small_primes[$_] } @order;
+  is_deeply(\@got, \@exp, "primes 0 .. $#small_primes can be randomly selected");
 
-  # Forwards and backwards
-  foreach my $index (0 .. $#small_primes) {
-    my $rindex = $#small_primes - $index;
-    is($primes[$index], $small_primes[$index], "primes[$index] == $small_primes[$index]");
-    is($primes[$rindex], $small_primes[$rindex], "primes[$rindex] == $small_primes[$rindex]");
-  }
+  # Forwards
+  @order = (0 .. $#small_primes);
+  @got = map { $primes[$_] } @order;
+  @exp = map { $small_primes[$_] } @order;
+  is_deeply(\@got, \@exp, "primes 0 .. $#small_primes in forward order");
+
+  # Backwards
+  @order = reverse (0 .. $#small_primes);
+  @got = map { $primes[$_] } @order;
+  @exp = map { $small_primes[$_] } @order;
+  is_deeply(\@got, \@exp, "primes 0 .. $#small_primes in reverse order");
 }
 
 {
@@ -88,6 +98,7 @@ plan tests => (3*scalar @small_primes) + 2 + scalar(keys %test_indices) + 8;
   }
 }
 
+# Test shifting
 {
   my @primes;  tie @primes, 'Math::Prime::Util::PrimeArray';
   is( shift @primes, 2, "shift 2");
diff --git a/t/80-pp.t b/t/80-pp.t
index 5dfdfa9..b1ff085 100644
--- a/t/80-pp.t
+++ b/t/80-pp.t
@@ -20,8 +20,6 @@ my @small_primes = qw/
 947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051
 1061 1063 1069
 /;  # next prime is 1087
-my %small_primes;
-map { $small_primes{$_} = 1; } @small_primes;
 
 my @primes = qw/
 1129 1327 9551 15683 19609 31397 155921
@@ -168,7 +166,7 @@ my %nthprimes32 = (
           100000000 => 2038074743,
 );
 my %nthprimes_small = map { $_ => $nthprimes32{$_} }
-                      grep {$_ <= 200000}
+                      grep { $extra ? ($_ <= 2_000_000) : ($_ <= 20_000) }
                       keys %nthprimes32;
 
 my %eivals = (
@@ -216,14 +214,14 @@ my %rvals = (
 
 
 plan tests => 1 +
-              1*(1087 + @primes + @composites) +
+              2 +
               3 + scalar(keys %small_single) + scalar(keys %small_range) +
-              2*scalar(keys %primegaps) + 8 + 148 + 148 + 1 +
+              2*scalar(keys %primegaps) + 8 + 1 + 1 + 1 +
               scalar(keys %pivals_small) + scalar(keys %pi_intervals) +
               2*scalar(keys %pivals_small) + scalar(keys %nthprimes_small) +
-              4 + $num_pseudoprimes +
+              4 + scalar(keys %pseudoprimes) +
               scalar(keys %eivals) + scalar(keys %livals) + scalar(keys %rvals) +
-              scalar @primes + 3*scalar @composites +
+              1 + 1 +
               0;
 
 use Math::Prime::Util qw/primes prime_count_approx prime_count_lower/;
@@ -249,19 +247,18 @@ require_ok 'Math::Prime::Util::PP';
 
 ###############################################################################
 
-foreach my $n (0 .. 1086) {
-  if (defined $small_primes{$n}) {
-    is( is_prime($n), 2, "$n is prime");
-  } else {
-    ok(!is_prime($n), "$n is not prime");
-  }
-}
-
-foreach my $n (@primes) {
-  is( is_prime($n), 2, "$n is prime" );
+{
+  my %small_primes = map { $_ => 1 } @small_primes;
+  my @isprime = map { is_prime($_) } (0 .. 1086);
+  my @exprime = map { $small_primes{$_} ? 2 : 0 } (0 .. 1086);
+  is_deeply( \@isprime, \@exprime, "is_prime 0 .. 1086" );
 }
-foreach my $n (@composites) {
-  is( is_prime($n), 0, "$n is not prime" );
+{
+  my @isprime = map { is_prime($_) ? "$_ is prime" : "$_ is composite" }
+                @primes, @composites;
+  my @exprime =  map { "$_ is prime" } @primes;
+  push @exprime, map { "$_ is composite" } @composites;
+  is_deeply( \@isprime, \@exprime, "is_prime for selected numbers" );
 }
 
 ###############################################################################
@@ -301,11 +298,15 @@ if ($use64) {
   is( next_prime(4294967291), 0, "Next prime of ~0-4 returns 0" );
 }
 
-foreach my $n (2010733 .. 2010880) {
-  is(next_prime($n), 2010881, "next_prime($n) == 2010881");
+{
+  my @exprime = map { "next_prime($_) == 2010881" }         (2010733..2010880);
+  my @isprime = map { "next_prime($_) == ".next_prime($_) } (2010733..2010880);
+  is_deeply(\@isprime, \@exprime, "next_prime for 148 primes before primegap end 2010881");
 }
-foreach my $n (2010734 .. 2010881) {
-  is(prev_prime($n), 2010733, "prev_prime($n) == 2010733");
+{
+  my @exprime = map { "prev_prime($_) == 2010733" }         (2010734..2010881);
+  my @isprime = map { "prev_prime($_) == ".prev_prime($_) } (2010734..2010881);
+  is_deeply(\@isprime, \@exprime, "prev_prime for 148 primes before primegap start 2010733");
 }
 # Similar test case to 2010870, where m=0 and next_prime is at m=1
 is(next_prime(1234567890), 1234567891, "next_prime(1234567890) == 1234567891)");
@@ -341,9 +342,10 @@ is( miller_rabin(2, 2), 1, "MR with 2 shortcut prime");
 is( miller_rabin(3, 2), 1, "MR with 3 shortcut prime");
 
 while (my($base, $ppref) = each (%pseudoprimes)) {
-  foreach my $p (@$ppref) {
-    ok(miller_rabin($p, $base), "Pseudoprime (base $base) $p passes MR");
-  }
+  my $npseudos = scalar @$ppref;
+  my @expmr = map { 1 } @$ppref;
+  my @gotmr = map { miller_rabin($_, $base) } @$ppref;
+  is_deeply(\@gotmr, \@expmr, "$npseudos pseudoprimes (base $base)");
 }
 
 ###############################################################################
@@ -360,32 +362,40 @@ while (my($n, $rin) = each (%rvals)) {
 
 ###############################################################################
 
-foreach my $n (@primes) {
-  my @f = factor($n);
-  is_deeply( \@f, [$n], "factor prime $n yields $n" );
+#foreach my $n (@primes) {
+#  my @f = factor($n);
+#  is_deeply( \@f, [$n], "factor prime $n yields $n" );
+#}
+{
+  my $ntests = scalar @primes;
+  my @expfactor = map { "$_" } @primes;
+  my @gotfactor = map { join(' * ', factor($_)) } @primes;
+  is_deeply( \@gotfactor, \@expfactor, "test factoring for $ntests primes");
 }
-foreach my $n (@composites) {
-  my @f = factor($n);
-  my $facstring = join(' * ', @f);
-
-  # Special case for 0 and 1
-  if ($n < 2) {
-    cmp_ok( scalar @f, '==', 1, "Factored small $n into itself" );
-    is( $f[0], $n, "$n = [ $facstring ]" );
-    ok( !is_prime($f[0]), "All factors [ $facstring ] of small $n are not prime" );
-    next;
+{
+  my $ntests = scalar @composites;
+  my @expfactor = map { "$_ factored correctly" } @composites;
+  my @gotfactor;
+
+  foreach my $n (@composites) {
+    my @f = factor($n);
+    my $facstring = join(' * ', @f);
+
+    if ($n < 2) {
+      push @gotfactor, (@f == 1 && $f[0] == $n)
+                       ? "$n factored correctly"
+                       : "$n not correct: $facstring";
+      next;
+    }
+    my $product  = 1; $product = int($product * $_) for @f;
+    my $allprime = 1; $allprime *= is_prime($_) for @f;
+    if (@f >= 2 && $product == $n && $allprime) {
+      push @gotfactor, "$n factored correctly";
+    } else {
+      push @gotfactor, "$n not correct: $facstring";
+    }
   }
-
-  # These are composites, so they should give us more than one factor
-  cmp_ok( scalar @f, '>=', 2, "Factored $n into multiple factors" );
-
-  # Do they multiply to the number?
-  my $product = 1;  $product = int($product * $_) for @f;
-  is( $product, $n, "$n = [ $facstring ]" );
-
-  # Are they all prime?
-  my $isprime = 1; $isprime *= is_prime($_) for @f;
-  ok( $isprime, "All factors [ $facstring ] of $n are prime" );
+  is_deeply( \@gotfactor, \@expfactor, "test factoring for $ntests composites");
 }
 
 ###############################################################################
diff --git a/t/81-bignum.t b/t/81-bignum.t
index f732d8f..45a7179 100644
--- a/t/81-bignum.t
+++ b/t/81-bignum.t
@@ -58,23 +58,26 @@ my %allfactors = (
   '23489223467134234890234680' => [qw/2 4 5 8 10 20 40 4073 4283 8146 8566 16292 17132 20365 21415 32584 34264 40730 42830 81460 85660 162920 171320 17444659 34889318 69778636 87223295 139557272 174446590 348893180 697786360 33662485846146713 67324971692293426 134649943384586852 168312429230733565 269299886769173704 336624858461467130 673249716922934260 1346499433845868520 137107304851355562049 144176426879046371779 274214609702711124098 288352853758092743558 548429219405422248196 576705 [...]
 );
 
-plan tests => 0 +
-              2*(@primes + @composites) +
-              1 +
-              2 +
-              1 +
-              $num_pseudoprime_tests +
-              5 +  # PC lower, upper, approx
-              scalar(keys %factors) +
-              scalar(keys %allfactors) +
-              2 +  # moebius, euler_phi
-              12 +  # random primes
-              0;
+plan tests =>  0
+             + 2*(@primes + @composites)
+             + 1   # primes
+             + 2   # next/prev prime
+             + 1   # primecount large base small range
+             + scalar(keys %pseudoprimes)
+             + 6   # PC lower, upper, approx
+             + 6*2*$extra # more PC tests
+             + scalar(keys %factors)
+             + scalar(keys %allfactors)
+             + 2   # moebius, euler_phi
+             + 12  # random primes
+             + 0;
 
 # Using GMP makes these tests run about 2x faster on some machines
 use bigint try => 'GMP';   #  <--------------- large numbers ahead!  > 2^64
+use Math::BigFloat;
 
 use Math::Prime::Util qw/
+  prime_set_config
   is_prob_prime
   prime_count_lower
   prime_count_upper
@@ -106,7 +109,7 @@ use Math::Prime::Util qw/
 #        LogarithmicIntegral
 #        RiemannR
 
-# Test bignum using PP
+# See if we're testing bignum with PP or GMP.
 diag "Math::Prime::Util::GMP not being used.\n"
   unless Math::Prime::Util::prime_get_config->{gmp};
 
@@ -146,24 +149,20 @@ is( prime_count(877777777777777777777752, 877777777777777777777872), 2, "prime_c
 
 while (my($psrp, $baseref) = each (%pseudoprimes)) {
   SKIP: {
-    skip "Your 64-bit Perl is broken, skipping pseudoprime tests for $psrp", scalar @$baseref if $broken64 && $psrp == 3825123056546413051;
-    foreach my $base (@$baseref) {
-      ok( is_strong_pseudoprime($psrp, $base), "$psrp is a strong pseudoprime to base $base" );
-    }
+    skip "Your 64-bit Perl is broken, skipping pseudoprime tests for $psrp", 1 if $broken64 && $psrp == 3825123056546413051;
+    my $baselist = join(",", @$baseref);
+    my @expmr = map { 1 } @$baseref;
+    my @gotmr = map { is_strong_pseudoprime($psrp, $_) } @$baseref;
+    is_deeply(\@gotmr, \@expmr, "$psrp is a strong pseudoprime to bases $baselist");
   }
 }
 
 ###############################################################################
 
-{
-  # See: http://www.mersenneforum.org/showpost.php?p=206983&postcount=25
-  my $n = 31415926535897932384626433;
-  cmp_ok( prime_count_lower($n), '<=', 544551456594153032339707, "PC lower (high)" );
-  cmp_ok( prime_count_lower($n), '>=', 544503356940764609324440, "PC lower (low)" );
-  cmp_ok( prime_count_upper($n), '>=', 544551456620339227350566, "PC upper (low)" );
-  cmp_ok( prime_count_upper($n), '<=', 544613583498498996743730, "PC upper (high)" );
-  # TODO: Need to improve accuracy for this
-  ok( abs(prime_count_approx($n) - 544551456607147153724423) < 50_000_000, "PC approx" );
+check_pcbounds(31415926535897932384, 716115441142294636, '8e-5', '2e-8');
+if ($extra) {
+  check_pcbounds(314159265358979323846, 6803848951392700268, '7e-5', '5e-9');
+  check_pcbounds(31415926535897932384626433, 544551456607147153724423, '4e-5', '3e-11');
 }
 
 ###############################################################################
@@ -211,3 +210,42 @@ cmp_ok( $randprime, '<', 2**80, "random 80-bit Maurer prime isn't too big");
 ok( is_prime($randprime), "random 80-bit Maurer prime is prime");
 
 ###############################################################################
+
+
+sub check_pcbounds {
+  my ($n, $expn, $percent, $percentrh) = @_;
+  $percent   = Math::BigFloat->new($percent);
+  $percentrh = Math::BigFloat->new($percentrh);
+
+  my $pcap = prime_count_approx($n);
+  is( $pcap, "$expn", "PC approx($n)" );
+
+  my $pclo = prime_count_lower($n);
+  my $pcup = prime_count_upper($n);
+  prime_set_config(assume_rh=>1);
+  my $pclo_rh = prime_count_lower($n);
+  my $pcup_rh = prime_count_upper($n);
+  prime_set_config(assume_rh=>0);
+
+  #diag "lower:    " . $pclo->bstr() . "  " . ($pcap-$pclo)->bstr;
+  #diag "rh lower: " . $pclo_rh->bstr() . "  " . ($pcap-$pclo_rh)->bstr;
+  #diag "approx:   " . $pcap->bstr();
+  #diag "rh upper: " . $pcup_rh->bstr() . "  " . ($pcup_rh-$pcap)->bstr;
+  #diag "upper:    " . $pcup->bstr() . "  " . ($pcup-$pcap)->bstr;
+  # lower:    544534406675337676203117  17049931809477521306
+  # rh lower: 544551456594152957592704  12994196131719
+  # approx:   544551456607147153724423
+  # rh upper: 544551456620339152603564  13191998879141
+  # upper:    544586259732074697890498  34803124927544166075
+
+  ok( $pclo <= $pclo_rh && $pclo_rh <= $pcap &&
+      $pcap <= $pcup_rh && $pcup_rh <= $pcup,
+      "prime count bounds for $n are in the right order");
+
+  my $pcapf = Math::BigFloat->new($pcap);
+#diag "" . ($pcapf - $pclo_rh)/($pcapf) . "  " . $percentrh/100.0 . "";
+  cmp_ok( ($pcapf - $pclo_rh)/$pcapf, '<=', $percentrh , "PC lower with RH");
+  cmp_ok( ($pcup_rh - $pcapf)/$pcapf, '<=', $percentrh , "PC upper with RH");
+  cmp_ok( ($pcapf - $pclo)/$pcapf,    '<=', $percent   , "PC lower");
+  cmp_ok( ($pcup - $pcapf)/$pcapf,    '<=', $percent   , "PC upper");
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libmath-prime-util-perl.git



More information about the Pkg-perl-cvs-commits mailing list