Message Boards Message Boards


How to interrupt an evaluation based on a time constraint

Posted 9 years ago
2 Replies
0 Total Likes

I'm looking for an instance of a prime for numbers of the form (10^n 78880-1)/3, where n is a positive integer. As a preliminary, it may be noted that n must be a multiple of 12. There are many other constraints based on division by small factors but since PrimeQ has a built-in small-factor trial-division subroutine, ultimately I am interested in those n that escape that subroutine, i.e. n where PrimeQ[(10^n 78880-1)/3] takes more than a second or so to evaluate. For example, the numbers 10008, 10032, 10068, 10140, 10188, 10320, 10332, 10368, 10392, 10404, 10584, 10608, 10620, 10644, 10692, 10764, 10824, 10872, 10932, 10980, 11004, ... I'd like to extend that list to, say, 10^6. How do I interrupt an evaluation that is taking too long so that I might move on to another candidate?

POSTED BY: Hans Havermann
2 Replies

Pretty sure you just want TimeConstrained:

TimeConstrained[ code, limit] 

That will return as $Aborted, but you can also return a different failure case with the 3rd argument:

TimeConstrained[Do[i^2, {i, 1000000}], 1, $Failed]
POSTED BY: Nick Lariviere
Anonymous User
Anonymous User
Posted 9 years ago

Yes. That worked, thank you! Can someone confirm that Mathematica 10 is significantly less astute at PrimeQ than Mathematica 9? Timing[PrimeQ[(10^9600 78880-1)/3]] yields {7.265538,False} in 10 and {0.003482,False} in 9 (on a four-year-old iMac running Yosemite). That strikes me as a bug.

POSTED BY: Anonymous User
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract