-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab2.rkt
110 lines (97 loc) · 2.35 KB
/
lab2.rkt
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
#lang racket
(define (sum-divisors number)
(define (sum-divisors-help i sum)
(if (< i number)
(if (= (remainder number i) 0)
(sum-divisors-help (+ i 1) (+ i sum))
(sum-divisors-help (+ i 1) sum))
sum
)
)
(sum-divisors-help 1 0)
)
(sum-divisors 6)
(define (increasing-digits? number)
(if (< number 10)
#t
(if (< (remainder number 10) (quotient (remainder number 100) 10))
#f
(increasing-digits? (quotient number 10))
)
)
)
(increasing-digits? 12134)
(define (binary-to-decimal number)
(define (binary-to-decimal-help number sum power)
(if (<= number 0)
sum
(binary-to-decimal-help
(quotient number 10)
(+ sum (*
(remainder number 10)
power)
)
(* power 2)
)
)
)
(binary-to-decimal-help number 0 1)
)
(binary-to-decimal 1001)
(define (contains? a b)
(define (contains-help num-holder b-left)
(if (= b-left 0)
#t
(if (= (remainder num-holder 10) (remainder b-left 10))
(contains-help (quotient num-holder 10) (quotient b-left 10))
#f
)
)
)
(if (< a b)
#f
(if (contains-help a b)
#t
(contains? (quotient a 10) b)
)
)
)
(contains? 123 23)
(define (palindrome? number)
(define (find-pow number n)
(if (= number 0)
n
(find-pow (quotient number 10) (+ n 1))
)
)
(define (palindrome?-help number n)
(if (and (< number 10) (<= n 1))
#t
(if (=
(quotient number (expt 10 (- n 1)))
(remainder number 10)
)
(palindrome?-help
(quotient (remainder number (expt 10 (- n 1))) 10)
(- n 2)
)
#f
)
)
)
(palindrome?-help number (find-pow number 0))
)
(palindrome? 1331)
(define (contains-palindrome? number)
(if (> number 10)
(if (or
(or (palindrome? (remainder number 100)) (= (remainder number 100) 0))
(or (palindrome? (remainder number 1000)) (= (remainder number 1000) 0))
)
#t
(contains-palindrome? (quotient number 10))
)
#f
)
)
(contains-palindrome? 10021)