chiark / gitweb /
Use known bound data to refine the search space.
authorSimon Tatham <anakin@pobox.com>
Fri, 14 Mar 2014 22:59:16 +0000 (22:59 +0000)
committerSimon Tatham <anakin@pobox.com>
Fri, 14 Mar 2014 22:59:39 +0000 (22:59 +0000)
partition.py

index 18a3a3b09ad4f862933cd19e48459d5f7f80e996..b539d32ec38b8d97c902c2872109d0428aa18179 100755 (executable)
@@ -3,6 +3,8 @@
 import sys, subprocess, math, getopt
 from fractions import Fraction
 
+import bounds
+
 verbose = False
 def vprint(*args):
     # In verbose mode, print diagnostics.
@@ -127,12 +129,15 @@ def search(n, m):
         # Trivial special case.
         return (m, 1, {(m,):n}, {(m,)*(n/m):m})
     best = (0,)
+    bound, _ = bounds.upper_bound(n, m)
     for d in xrange(1, n+1):
-        for k in xrange(m*d/2, int(math.ceil(best[0]*d))-1, -1):
+        for k in xrange(int(bound*d), int(math.ceil(best[0]*d))-1, -1):
             result = try_one_minfrag(n, m, k, d)
             if result is not None:
                 best = (Fraction(k, d), d) + result
                 break
+        if best == bound:
+            break # terminate early if we know it's the best answer
     return best
 
 def search_and_report(n, m):