Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is possible to use Bit-vector arithmetic to solve Diophantine equations. The basic idea is to use <code>ZeroExt</code> to avoid the overflows pointed out by Pad. For example, if we are multiplying two bit-vectors <code>x</code> and <code>y</code> of size <code>n</code>, then we must add <code>n</code> zero bits to <code>x</code> and <code>y</code> to make sure that the result will not overflow. That is, we write:</p> <pre><code> ZeroExt(n, x) * ZeroExt(n, y) </code></pre> <p>The following set of Python functions can be used to "safely" encode any Diophantine equation <code>D(x_1, ..., x_n) = 0</code> into Bit-vector arithmetic. By "safely", I mean if there is a solution that fits in the number of bits used to encode <code>x_1</code>, ..., <code>x_n</code>, then it will eventually be found modulo resources such as memory and time. Using these function, we can encode the Pell's equation <code>x^2 - d*y^2 == 1</code> as <code>eq(mul(x, x), add(val(1), mul(val(d), mul(y, y))))</code>. The function <code>pell(d,n)</code> tries to find values for <code>x</code> and <code>y</code> using <code>n</code> bits.</p> <p>The following link contains the complete example: <a href="http://rise4fun.com/Z3Py/Pehp">http://rise4fun.com/Z3Py/Pehp</a></p> <p>That being said, it is super expensive to solve Pell's equation using Bit-vector arithmetic. The problem is that multiplication is really hard for the bit-vector solver. The complexity of the encoding used by Z3 is quadratic on <code>n</code>. On my machine, I only managed to solve Pell's equations that have small solutions. Examples: <code>d = 982</code>, <code>d = 980</code>, <code>d = 1000</code>, <code>d = 1001</code>. In all cases, I used a <code>n</code> smaller than <code>24</code>. I think there is no hope for equations with very big minimal positive solutions such as <code>d = 991</code> where we need approximately 100 bits to encode the values of <code>x</code> and <code>y</code>. For these cases, I think a specialized solver will perform much better.</p> <p>BTW, the rise4fun website has a small timeout, since it is a shared resource used to run all research prototypes in the Rise group. So, to solve non trivial Pell's equations, we need to run Z3 on our own machines.</p> <pre><code>def mul(x, y): sz1 = x.sort().size() sz2 = y.sort().size() return ZeroExt(sz2, x) * ZeroExt(sz1, y) def add(x, y): sz1 = x.sort().size() sz2 = y.sort().size() rsz = max(sz1, sz2) + 1 return ZeroExt(rsz - sz1, x) + ZeroExt(rsz - sz2, y) def eq(x, y): sz1 = x.sort().size() sz2 = y.sort().size() rsz = max(sz1, sz2) return ZeroExt(rsz - sz1, x) == ZeroExt(rsz - sz2, y) def num_bits(n): assert(n &gt;= 0) r = 0 while n &gt; 0: r = r + 1 n = n / 2 if r == 0: return 1 return r def val(x): return BitVecVal(x, num_bits(x)) def pell(d, n): x = BitVec('x', n) y = BitVec('y', n) solve(eq(mul(x,x), add(val(1), mul(val(d), mul(y, y)))) , x &gt; 0, y &gt; 0) </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