aboutsummaryrefslogtreecommitdiff
path: root/consecutive-fibonacci-numbers-are-coprime.lisp
blob: 0146dc17bef7c7be841d070e77ef4513e7c8c79b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
;;; consecutive-fibonacci-numbers-are-coprime.lisp: 隣り合うフィボナッチ数は互いに素である
(in-package "ACL2")

;; 数字を足したり引いたりする関数に関する証明をするときに便利な Book
(include-book "arithmetic/top-with-meta" :dir :system)

;; フィボナッチ関数の定義
(defun fibonacci (n)
  (declare (xargs :guard (natp n)))
  (cond
    ((zp n) 0)
    ((equal n 1) 1)
    (t (+ (fibonacci (- n 1))
          (fibonacci (- n 2))))))

;; ユークリッドの互除法による最大公約数の定義
(defun gcd$ (n m)
  (declare (xargs :measure (+ (nfix m) (nfix n))
                  :guard (and (posp n)
                              (posp m))))
  (if (and (posp n) (posp m))
      (cond
        ((= n m)
         n)
        ((< n m) (gcd$ (- m n) n))
        (t (gcd$ (- n m) m)))
      0))

;; gcd$ の結果は正の整数である
(defthm posp-gcd$
  (implies (and (posp m)
                (posp n))
           (posp (gcd$ m n))))

;; c が n と m の公約数であるかを調べる述語
(defmacro common-divisor-p (c n m)
  `(and (integerp (* ,n (/ ,c)))
        (integerp (* ,m (/ ,c)))))

;; gcd-is-common-divisor の補助定理
(defthm gcd-is-common-divisor-lemma-1
  (implies (common-divisor-p c (+ (- a) b) a)
           (integerp (* b (/ c))))
  :rule-classes ((:rewrite :match-free :all)))

;; gcd-is-common-divisor の補助定理
(defthm gcd-is-common-divisor-lemma-2
  (implies (common-divisor-p c (+ a (- b)) b)
           (integerp (* a (/ c))))
  :rule-classes ((:rewrite :match-free :all)))

;; (gcd$ n m) は n と m の公約数である
(defthm gcd-is-common-divisor
  (implies (and (natp n)
                (natp m))
           (common-divisor-p (gcd$ m n) n m)))

;; n と m が互いに素かどうかを調べる述語
(defmacro coprimep (n m)
  `(equal (gcd$ ,n ,m) 1))

;; 隣り合うフィボナッチ数は互いに素である
(defthm consecutive-fibonacci-numbers-are-coprime
  (implies (posp n)
           (coprimep (fibonacci n)
                     (fibonacci (+ n 1)))))