problem39

  • a^2+b^2+c^2にa+b+c=pを代入して式を作成。
  • pは必ず偶数

ということで

(use srfi-42)

(define (ok? p) ;; a,b,c : a < b < c
  (any?-ec (: b p) (: a b) 
           (zero? (+ (* p p) (- (* 2 (+ a b) p)) (* 2 a b)))))

(max-ec (: p 120 1000 2) (if (ok? p)) p)

以下pが偶数になることを確かめる

p is even

符号のみの計算で確かめる。

(define-syntax define-calc
  (syntax-rules ()
    [(_ (name1 name) odd-odd odd-even even-odd even-even)
     (begin
       (define (name1 x y)
         (cond [(eq? 'odd x) (if (eq? 'odd y) odd-odd odd-even)]
               [(eq? 'even x) (if (eq? 'odd y) even-odd even-even)]))
       (define (name . args)
         (fold name1 (car args) (cdr args))))]))

(define-calc (add1 add) 'even 'odd 'odd 'even)
(define-calc (mul1 mul) 'odd 'even 'even 'even)

;; p is a + b + c (c^2 = a^2 + b^2)
;; odd * odd =~ odd; even * even =~ even;
;; p =~ op(a) + op(b) + op(c)
;; p =~ op(a) + op(b) + op(c^2)
;; p =~ op(a) + op(b) + op(a^2+b^2)
(define (check a b)
  (add (mul a a) (mul b b) 
       (add (mul a a) (mul b b)))) ;; c^2

(let1 args '(odd even)
  (every?-ec (: i args) (: j args) (eq? 'even (check i j)))) ; => #t

普通にpが偶数なことって自明なのかなー?