-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path36.ss
More file actions
33 lines (27 loc) · 815 Bytes
/
36.ss
File metadata and controls
33 lines (27 loc) · 815 Bytes
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
#lang scheme
(require math/number-theory
"digits.ss")
(define (binary-digits n)
;; rewrite of soegaard's "digits", but with 2 instead of 10. He
;; really needs to parameterize the base, or allow me to pass it in
;; as an argument.
(define (d x)
(if (< x 2)
(list x)
(cons (remainder x 2)
(d (quotient x 2)))))
(unless (integer? n)
(error 'digits "expected an integer, got: n"))
(reverse (d (if (negative? n) (- n) n))))
(define (palindrome? seq)
(equal? seq (reverse seq)))
(define (base-2-palindrome? x)
(palindrome? (binary-digits x)))
(define (base-10-palindrome? x)
(palindrome? (digits x)))
(for/fold ([sum 0])
([n (in-range 1000000)])
(if (and (base-2-palindrome? n)
(base-10-palindrome? n))
(+ n sum)
sum))