changeset 31398:4f86d56c090d

__isprimelarge__.cc: Guard against a rare loop runaway condition. __isprimelarge.cc__: Add a practical guard against a rare loop runaway. Without this check, `factor (1084978968791)` can take a very long time to complete. With it, it takes 0.16 seconds. Add that input as a BIST.
author Arun Giridhar <arungiridhar@gmail.com>
date Sat, 05 Nov 2022 18:35:09 -0400
parents 0bd5ea4bd31c
children 4dc9230db992
files libinterp/corefcn/__isprimelarge__.cc
diffstat 1 files changed, 2 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/__isprimelarge__.cc	Sat Nov 05 11:31:56 2022 -0400
+++ b/libinterp/corefcn/__isprimelarge__.cc	Sat Nov 05 18:35:09 2022 -0400
@@ -238,7 +238,7 @@
           j <<= 1;
         }
 
-      if (g == n)  // restart with a different c
+      if (g == n || i > 1000000ULL)  // cut losses, restart with a different c
         return pollardrho (n, c + 2);
 
       if (g > 1ULL)  // found GCD ==> exit loop properly
@@ -275,6 +275,7 @@
 
 /*
 %!assert (__pollardrho__ (uint64 (78567695338254293)), uint64 (443363))
+%!assert (__pollardrho__ (1084978968791), uint64 (832957))
 
 %!error <unable to convert input> (__pollardrho__ ({'foo'; 'bar'}))
 */