-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpract2.rkt
96 lines (84 loc) · 1.38 KB
/
pract2.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
#lang racket
(define (prime? n)
(define (prime-helper? n divider)
(if
(= n divider)
#t
(if
(= (remainder n divider) 0)
#f
(prime-helper? n (+ divider 1))
)
)
)
(prime-helper? n 2)
)
(prime? 4)
(define (sum-interval a b)
(define (sum-interval-helper a b curSum)
(if
(> a b)
curSum
(sum-interval-helper
(+ a 1)
b
(+ curSum a)
)
)
)
(sum-interval-helper a b 0)
)
(sum-interval 1 10)
(define (sum-interval-recursive a b)
(if
(= a b)
a
(+
a
(sum-interval-recursive (+ a 1) b)
)
)
)
(sum-interval-recursive 1 10)
(define (fib n)
(cond ((= 0 n) 1)
((= 1 n) 2)
(else (+ (fib (- n 1)) (fib (- n 2))))))
(define (fibonacci-iter n)
(define (fib-help x1 x2 num)
(if
(= num n)
(+ x1 x2)
(fib-help x2 (+ x1 x2) (+ num 1))
)
)
(fib-help 0 1 0)
)
(time (fib 35))
(time (fibonacci-iter 35))
;
;(define (ackerman x y)
; (define (ackerman-help x y acc)
; (cond
; (
; (= 0 x) (+ y 1)
; )
; (
; (= 0 y) (ackerman (- x 1) 1)
; )
;(else
;(
(define (ackerman x y)
(cond
(
(= 0 x) (+ y 1)
)
(
(= 0 y) (ackerman (- x 1) 1)
)
(else
(ackerman (- x 1) (ackerman x (- y 1)))
)
)
)
(ackerman 4 2)