```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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 ``` ```\$ cat sxp.rkt #lang racket (require racket/set) (define (smallest-at-least-po2 a b) ; Return the smallest power of 2 that is at least the given ; values. (define (bit-size i) ; Return the smallest number of bits required to represent i. (if (< i 2) 1 (let ((n (inexact->exact (ceiling (/ (log i) (log 2)))))) (+ n (if (= (expt 2 n) i) 1 0))))) (expt 2 (max (bit-size a) (bit-size b)))) ; These functions treat numbers as n-bit values, where n is the ; smallest number of bits capable of representing the sum and xor ; values. They also ignore overflow, which means it's possible ; to have pair values that are larger than the sum value but ; still add to the sum value. Also, because xor and sum are ; commutative, the number pairs are created with the smaller ; value first (on the left, the car). (define (sum-xor-pairs-n ab-sum ab-xor) ; Return a set of number pairs such that, for each pair, the ; sum of the two numbers equals ab-sum (ignoring overflow) and ; the xor of the two numbers equlas ab-xor. ; This function does work proportional to the power of the ; larger of the smallest number of bits needed to represent the ; sum and xor values, which is roughly linear in the larger of ; the sum and xor values. ; This function essentially implements the truth tables sum and ; xor: ; ; Ci a b Co s x ; 0 0 0 0 0 0 ; 0 0 1 0 1 1 ; 0 1 0 0 1 1 ; 0 1 1 1 0 0 ; 1 0 0 0 1 0 ; 1 0 1 1 0 1 ; 1 1 0 1 0 1 ; 1 1 1 1 1 0 ; ; Ci the carry-in bit ; a a bit from one of the pair values ; b the corresponding bit from the other pair value ; Co the carry-out from Ci + a + b ; s Ci + a + b ; x a xor b ; ; Interesting things to note about this table: ; ; The s and x bits are the same when Ci = 0. ; The s and x bits are different when Ci = 1. ; xor ignores Ci (and Co). (define (add-msb a-msb b-msb ab-pairs) ; Add the given most-significant bits to the given set of ; number pairs; return the new set. (for/set ([v ab-pairs]) (vector (cons a-msb (vector-ref v 0)) (cons b-msb (vector-ref v 1))))) (define (vector->pair ab-pairs) ; Return a set of number pairs, where each number corresponds ; to a bit list in the given pairs and each pair corresponds ; to a vector in the given set. The smaller value appears ; first in the pair. (define (implode bit-list) ; Return the number equivalent to the given bit list (msb on ; the left). (let loop ((n 0) (bit-list bit-list)) (if (null? bit-list) n (loop (+ (* n 2) (car bit-list)) (cdr bit-list))))) (for/set ((v ab-pairs)) (let ((a (implode (vector-ref v 0))) (b (implode (vector-ref v 1)))) (cons (min a b) (max a b))))) (define (oops emsg) (raise-arguments-error 'sum-xor-pairs-n "some unfathomable error")) (vector->pair (let loop ((ab-sum ab-sum) (ab-xor ab-xor) (carry-in 0) (ab-pairs (if (and (= ab-sum 0) (= ab-xor 0)) (set #((0) (0)) #((1) (1))) (set #(() ()))))) (if (and (= ab-sum 0) (= ab-xor 0)) ab-pairs (let ((sum-bit (remainder ab-sum 2)) (xor-bit (remainder ab-xor 2)) (ab-sum (quotient ab-sum 2)) (ab-xor (quotient ab-xor 2))) (cond ((= carry-in 0) (cond ((and (= sum-bit 0) (= xor-bit 0)) (set-union (loop ab-sum ab-xor 0 (add-msb 0 0 ab-pairs)) (loop ab-sum ab-xor 1 (add-msb 1 1 ab-pairs)))) ((and (= sum-bit 1) (= xor-bit 1)) (set-union (loop ab-sum ab-xor 0 (add-msb 0 1 ab-pairs)) (loop ab-sum ab-xor 0 (add-msb 1 0 ab-pairs)))) ((not (= sum-bit xor-bit)) ; If the carry-in's zero, the sum of the two ; bits (ignoring carry-out) must equal the xor ; of the two bits. Because that's not the ; case, there can be no solutions down this ; branch. (set)) (#t (oops)))) ((= carry-in 1) (cond ((and (= sum-bit 1) (= xor-bit 0)) (set-union (loop ab-sum ab-xor 0 (add-msb 0 0 ab-pairs)) (loop ab-sum ab-xor 1 (add-msb 1 1 ab-pairs)))) ((and (= sum-bit 0) (= xor-bit 1)) (set-union (loop ab-sum ab-xor 1 (add-msb 0 1 ab-pairs)) (loop ab-sum ab-xor 1 (add-msb 1 0 ab-pairs)))) ((= sum-bit xor-bit) ; If the carry-in's one, the sum of the two ; bits (ignoring carry-out) cannot equal the ; xor of the two bits. Because that's not the ; case, there can be no solutions down this ; branch. (set)) (#t (oops)))) (#t (oops)))))))) (define (sum-xor-pairs-nsq ab-sum ab-xor) ; Return a number-pair set such that, for each pair, the sum of ; the two numbers equals ab-sum (ignoring overflow) and the xor ; of the two numbers equlas ab-xor. ; This function does work proportinal to ab-sum*ab-xor ; (a.k.a. n-squared). (let ((N (smallest-at-least-po2 ab-sum ab-xor))) (let outer-loop ((a 0) (ab-pairs (set))) (if (= a N) ab-pairs (let inner-loop ((b a) (ab-pairs ab-pairs)) (if (= b N) (outer-loop (+ a 1) ab-pairs) (inner-loop (+ b 1) (if (and (= (remainder (+ a b) N) ab-sum) (= (bitwise-xor a b) ab-xor)) (set-add ab-pairs (cons a b)) ab-pairs)))))))) (sum-xor-pairs-n 9 5) (require rackunit) (define (check-sum-xor-pairs n) (define (check-sum-xor-list ab-pairs ab-sum ab-xor) (define N (smallest-at-least-po2 ab-sum ab-xor)) (define (check-sum-xor a b) (check-eq? (remainder (+ a b) N) ab-sum) (check-eq? (bitwise-xor a b) ab-xor)) (let loop ((ab-pairs ab-pairs)) (if (set-empty? ab-pairs) #t (let ((p (set-first ab-pairs))) (check-sum-xor (car p) (cdr p)) (loop (set-rest ab-pairs)))))) (do ((ab-sum 0 (+ 1 ab-sum))) ((> ab-sum n) #t) (do ((ab-xor 0 (+ 1 ab-xor))) ((> ab-xor n) #t) (let ((ab-pairs-n (sum-xor-pairs-n ab-sum ab-xor))) (check-sum-xor-list ab-pairs-n ab-sum ab-xor) (set=? ab-pairs-n (sum-xor-pairs-nsq ab-sum ab-xor)))))) (check-sum-xor-pairs 100) (define (time-it f n iters) (define (run-test) (do ((ab-sum 0 (+ 1 ab-sum))) ((> ab-sum n) #t) (do ((ab-xor 0 (+ 1 ab-xor))) ((> ab-xor n) #t) (f ab-sum ab-xor)))) (let loop ((t 0) (i 0)) (if (= i iters) (inexact->exact (round (/ t iters))) (let-values (((a b c d) (time-apply run-test '()))) (loop (+ t b) (+ i 1)))))) (let ((iters 3)) (do ((i 10 (+ 10 i))) ((> i 100) #t) (printf "sum-xor max: ~a, sum-xor-pairs-n: ~a, sum-xor-pairs-nsq: ~a\n" i (time-it sum-xor-pairs-n i iters) (time-it sum-xor-pairs-nsq i iters)))) \$ mzscheme sxp.rkt (set '(2 . 7) '(3 . 6) '(10 . 15) '(11 . 14)) #t sum-xor max: 10, sum-xor-pairs-n: 3, sum-xor-pairs-nsq: 1 sum-xor max: 20, sum-xor-pairs-n: 7, sum-xor-pairs-nsq: 3 sum-xor max: 30, sum-xor-pairs-n: 17, sum-xor-pairs-nsq: 9 sum-xor max: 40, sum-xor-pairs-n: 32, sum-xor-pairs-nsq: 39 sum-xor max: 50, sum-xor-pairs-n: 52, sum-xor-pairs-nsq: 80 sum-xor max: 60, sum-xor-pairs-n: 73, sum-xor-pairs-nsq: 128 sum-xor max: 70, sum-xor-pairs-n: 107, sum-xor-pairs-nsq: 296 sum-xor max: 80, sum-xor-pairs-n: 145, sum-xor-pairs-nsq: 547 sum-xor max: 90, sum-xor-pairs-n: 188, sum-xor-pairs-nsq: 827 sum-xor max: 100, sum-xor-pairs-n: 236, sum-xor-pairs-nsq: 1128 #t \$ ```