Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For the special case where set Y = [1..9] and n > 0:</p> <ul> <li>n &lt;= 9 : 0 operations</li> <li>n &lt;=18 : 1 operation (+)</li> <li>otherwise : Remove any divisor found in Y. If this is not enough, do a recursion on the remainder for all offsets -9 .. +9. Offset 0 can be skipped as it has already been tried.</li> </ul> <p>Notice how division is not needed in this case. For other Y this does not hold. </p> <p>This algorithm is exponential in log(n). The exact analysis is a job for somebody with more knowledge about algebra than I.</p> <p>For more speed, add pruning to eliminate some of the search for larger numbers.</p> <p>Sample code:</p> <pre><code>def findop(n, maxlen=9999): # Return a short postfix list of numbers and operations # Simple solution to small numbers if n&lt;=9: return [n] if n&lt;=18: return [9,n-9,'+'] # Find direct multiply x = divlist(n) if len(x) &gt; 1: mults = len(x)-1 x[-1:] = findop(x[-1], maxlen-2*mults) x.extend(['*'] * mults) return x shortest = 0 for o in range(1,10) + range(-1,-10,-1): x = divlist(n-o) if len(x) == 1: continue mults = len(x)-1 # We spent len(divlist) + mults + 2 fields for offset. # The last number is expanded by the recursion, so it doesn't count. recursion_maxlen = maxlen - len(x) - mults - 2 + 1 if recursion_maxlen &lt; 1: continue x[-1:] = findop(x[-1], recursion_maxlen) x.extend(['*'] * mults) if o &gt; 0: x.extend([o, '+']) else: x.extend([-o, '-']) if shortest == 0 or len(x) &lt; shortest: shortest = len(x) maxlen = shortest - 1 solution = x[:] if shortest == 0: # Fake solution, it will be discarded return '#' * (maxlen+1) return solution def divlist(n): l = [] for d in range(9,1,-1): while n%d == 0: l.append(d) n = n/d if n&gt;1: l.append(n) return l </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload