-
Notifications
You must be signed in to change notification settings - Fork 1
/
55.ss
38 lines (31 loc) · 780 Bytes
/
55.ss
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
#lang scheme
(require math/number-theory
"digits.ss"
(planet schematics/schemeunit:3))
(define (palindromic? n)
(= n (number-reverse n)))
(define (number-reverse n)
(digits->number (reverse (digits n))))
(define (lychrel? n)
(let loop ([n n]
[iterations 1])
(let ((new (+ n (number-reverse n))))
(cond
((palindromic? new)
#f)
((= 50 iterations)
#t)
(else
(loop new
(add1 iterations)))))))
(check-false (lychrel? 47))
(check-false (lychrel? 349))
(check-true (lychrel? 196))
(check-true (lychrel? 4994))
(check-true (lychrel? 10677))
(length
(for/fold ([lychrels '()])
[(n (in-range 10000))]
(if (lychrel? n)
(cons n lychrels)
lychrels)))