-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path37.rkt
More file actions
executable file
·49 lines (39 loc) · 1.18 KB
/
37.rkt
File metadata and controls
executable file
·49 lines (39 loc) · 1.18 KB
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
#lang racket
(require rackunit
(only-in math/number-theory prime? next-prime)
"digits.ss")
(define (sans-first-digit n)
(digits->number (cdr (digits n))))
(define (sans-last-digit n)
(digits->number (drop-right (digits n) 1)))
(define (left-truncatable? n)
(or (zero? n)
(and (prime? n)
(left-truncatable? (sans-first-digit n)))))
(define (right-truncatable? n)
(or (zero? n)
(and (prime? n)
(right-truncatable? (sans-last-digit n)))))
(define (truncatable? n)
(and (left-truncatable? n)
(right-truncatable? n)))
(define (eligible? n)
(and (< 7 n)
(truncatable? n)))
(check-true (eligible? 3797))
(check-false (eligible? 2))
(check-false (eligible? 3))
(check-false (eligible? 5))
(check-false (eligible? 7))
(let loop ([truncatable-primes '()]
[length 0]
[candidate (next-prime 0)])
(if (< length 11)
(if (eligible? candidate)
(loop (cons candidate truncatable-primes)
(add1 length)
(next-prime candidate))
(loop truncatable-primes
length
(next-prime candidate)))
(apply + truncatable-primes)))