```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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 ``` ```; gaussian integers, part 1 (define (gs a b) (cons a b)) (define (re x) (car x)) (define (im x) (cdr x)) (define (gauss a b) (when (not (integer? a)) (error 'gauss "must be integer")) (when (not (integer? b)) (error 'gauss "must be integer")) (gs a b)) (define (gauss-from-complex x) (gauss (real-part x) (imag-part x))) (define (gauss-to-complex x) (make-rectangular (re x) (im x))) (define (gauss-zero? x) (and (zero? (re x)) (zero? (im x)))) (define (gauss-unit? x) (or (and (= (abs (re x)) 1) (zero? (im x))) (and (zero? (re x)) (= (abs (im x)) 1)))) (define (gauss-conjugate x) (gs (re x) (- (im x)))) (define (gauss-norm x) (define (square x) (* x x)) (+ (square (re x)) (square (im x)))) (define (gauss-eql? x y) (and (= (re x) (re y)) (= (im x) (im y)))) (define (gauss-add . xs) (define (add x y) (gs (+ (re x) (re y)) (+ (im x) (im y)))) (let loop ((xs xs) (zs (gs 0 0))) (if (null? xs) zs (loop (cdr xs) (add (car xs) zs))))) (define (gauss-negate x) (gs (- (re x)) (- (im x)))) (define (gauss-sub . xs) (define (sub x y) (gs (- (re x) (re y)) (- (im x) (im y)))) (cond ((null? xs) (error 'gauss-sub "no operands")) ((null? (cdr xs)) (gauss-negate (car xs))) (else (let loop ((xs (cdr xs)) (zs (car xs))) (if (null? xs) zs (loop (cdr xs) (sub zs (car xs)))))))) (define (gauss-mul . xs) (define (mul x y) (gs (- (* (re x) (re y)) (* (im x) (im y))) (+ (* (re x) (im y)) (* (im x) (re y))))) (let loop ((xs xs) (zs (gs 1 0))) (if (null? xs) zs (loop (cdr xs) (mul (car xs) zs))))) (define (gauss-quotient num den) (let ((n (gauss-norm den)) (r (+ (* (re num) (re den)) (* (im num) (im den)))) (i (- (* (re den) (im num)) (* (re num) (im den))))) (gs (round (/ r n)) (round (/ i n))))) (define (gauss-remainder num den quo) (gauss-sub num (gauss-mul den quo))) ; gaussian integers, part 2 (define (gauss-gcd x y) (if (gauss-zero? y) x (let* ((q (gauss-quotient x y)) (r (gauss-remainder x y q))) (gauss-gcd y r)))) (define (gauss-divides? d n) (gauss-zero? (gauss-remainder n d (gauss-quotient n d)))) (define (gauss-coprime? x y) (gauss-unit? (gauss-gcd x y))) (define (gauss-prime? x) (cond ((gauss-unit? x) #f) ((zero? (re x)) (and (prime? (im x)) (= (modulo (im x) 4) 3))) ((zero? (im x)) (and (prime? (re x)) (= (modulo (re x) 4) 3))) (else (prime? (gauss-norm x))))) (define (gauss-factors x) (define (find-k p a) (if (= (expm a (/ (- p 1) 2) p) (- p 1)) (expm a (/ (- p 1) 4) p) (find-k p (+ a 1)))) (let loop ((g x) (qs (list)) (ps (factors (gauss-norm x)))) ;(display g) (display qs) (display ps) (newline) (cond ((null? ps) (if (gauss-eql? g (gs 1 0)) qs (cons g qs))) ((= (car ps) 2) (loop (gauss-quotient g (gs 1 1)) (cons (gs 1 1) qs) (cdr ps))) ((= (modulo (car ps) 4) 3) (let ((q (gs (car ps) 0))) (loop (gauss-quotient g q) (cons q qs) (cddr ps)))) (else (let* ((p (car ps)) (k (find-k p 2)) (u (gauss-gcd (gs p 0) (gs k 1))) (z (gauss-quotient g u)) (q (if (gauss-zero? (gauss-remainder g u z)) u (gauss-conjugate u)))) (loop z (cons q qs) (cdr ps))))))) ; library (define (expm b e m) (define (m* x y) (modulo (* x y) m)) (cond ((zero? e) 1) ((even? e) (expm (m* b b) (/ e 2) m)) (else (m* b (expm (m* b b) (/ (- e 1) 2) m))))) (define (last-pair xs) (if (null? (cdr xs)) xs (last-pair (cdr xs)))) (define (cycle . xs) (set-cdr! (last-pair xs) xs) xs) (define wheel (cons 1 (cons 2 (cons 2 (cycle 4 2 4 2 4 6 2 6))))) (define (prime? n) (let loop ((f 2) (ws wheel)) (cond ((< n (* f f)) #t) ((zero? (modulo n f)) #f) (else (loop (+ f (car ws)) (cdr ws)))))) (define (factors n) (let loop ((n n) (f 2) (ws wheel) (fs (list))) (cond ((< n (* f f)) (if (= n 1) fs (reverse (cons n fs)))) ((zero? (modulo n f)) (loop (/ n f) f ws (cons f fs))) (else (loop n (+ f (car ws)) (cdr ws) fs))))) (display (gauss-factors (gauss 361 -1767))) (newline) ```
 ```1 ``` ```((-7 . 2) (19 . 0) (4 . 1) (2 . 1) (1 . 1)) ```