使用Z3-SMTLIB找到由两个5位数字的乘积制成的最大回文

时间:2015-03-30 16:07:20

标签: z3 palindrome

我们会查找一些efghiihgfe表单,该表单是999ab99qcd两个数字的乘积。

我们使用以下代码

 (declare-const a Int)
(declare-const b Int)
(declare-const c Int)
(declare-const d Int)
(declare-const e Int)
(declare-const f Int)
(declare-const g Int)
(declare-const h Int)
(declare-const i Int)
(declare-const p Int)
(declare-const q Int)


(assert (and (>= a 0) (<= a 9)))
(assert (and (>= b 0) (<= b 9)))
(assert (and (>= c 0) (<= c 9)))
(assert (and (>= d 0) (<= d 9)))
(assert (and (>= e 0) (<= e 9)))
(assert (and (>= f 0) (<= f 9)))
(assert (and (>= g 0) (<= g 9)))
(assert (and (>= h 0) (<= h 9)))
(assert (and (>= i 0) (<= i 9)))
(assert (and (>= p 0) (<= p 9)))
(assert (and (>= q 0) (<= q 9)))


(assert (= (* (+ 99900 (* 10 a) b  )  (+ 99000 (* 100 q) (* 10 c)  d  ))      
           (+ (* (^ 10 9) e) (* (^ 10 8)  f) (* (^ 10 7) g) (* (^ 10 6) h) (* (^ 10 5)   i)    
              (* (^ 10 4)  i)  (* 1000 h ) (* 100 g) (* 10 f) e)    ) )




(check-sat)
(get-model)

(eval (+ (* (^ 10 9) e) (* (^ 10 8)  f) (* (^ 10 7) g) (* (^ 10 6) h) (* (^ 10 5)   i)    
              (* (^ 10 4)  i)  (* 1000 h ) (* 100 g) (* 10 f) e))

,输出

    (model
  (define-fun q () Int
    6)
  (define-fun p () Int
    0)
  (define-fun i () Int
    0)
  (define-fun h () Int
    6)
  (define-fun g () Int
    6)
  (define-fun f () Int
    9)
  (define-fun e () Int
    9)
  (define-fun d () Int
    1)
  (define-fun c () Int
    8)
  (define-fun b () Int
    9)
  (define-fun a () Int
    7)
)
9966006699

要验证9966006699是格言,我们运行代码

 (declare-const a Int)
(declare-const b Int)
(declare-const c Int)
(declare-const d Int)
(declare-const e Int)
(declare-const f Int)
(declare-const g Int)
(declare-const h Int)
(declare-const i Int)
(declare-const p Int)
(declare-const q Int)


(assert (and (>= a 0) (<= a 9)))
(assert (and (>= b 0) (<= b 9)))
(assert (and (>= c 0) (<= c 9)))
(assert (and (>= d 0) (<= d 9)))
(assert (and (>= e 0) (<= e 9)))
(assert (and (>= f 0) (<= f 9)))
(assert (and (>= g 0) (<= g 9)))
(assert (and (>= h 0) (<= h 9)))
(assert (and (>= i 0) (<= i 9)))
(assert (and (>= p 0) (<= p 9)))
(assert (and (>= q 0) (<= q 9)))


(assert (= (* (+ 99900 (* 10 a) b  )  (+ 99000 (* 100 q) (* 10 c)  d  ))      
           (+ (* (^ 10 9) e) (* (^ 10 8)  f) (* (^ 10 7) g) (* (^ 10 6) h) (* (^ 10 5)   i)    
              (* (^ 10 4)  i)  (* 1000 h ) (* 100 g) (* 10 f) e)    ) )


(assert  (> (+ (* (^ 10 9) e) (* (^ 10 8)  f) (* (^ 10 7) g) (* (^ 10 6) h) (* (^ 10 5)   i)    
              (* (^ 10 4)  i)  (* 1000 h ) (* 100 g) (* 10 f) e)   9966006699 ))

(check-sat)

,输出

unsat

如果Z3有更高效的程序来解决问题,请告诉我。

1 个答案:

答案 0 :(得分:1)

谢谢,也许使用位向量强制使用有限域而不是ILP。