-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.18.scm
42 lines (35 loc) · 798 Bytes
/
1.18.scm
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
(load "./1.17")
(define (mul a b)
(mul-iter a b 0))
(define (mul-iter a b p)
(cond ((= b 0) p)
((even? b) (mul-iter (double a) (halve b) p))
(else (mul-iter a (- b 1) (+ p a)))))
; ロシア農民の方法
;
; 45 * 113
;
; 45 113 +45
; 90 56
; 180 28
; 360 14
; 720 7 +720
; 1440 3 +1440
; 2880 1 +2880
;
; = 5085
(define (halve-floor x)
(truncate (/ x 2)))
(define (russian-peasant-mul a b)
(mul-iter a b 0))
(define (r-mul-iter a b p)
(cond ((= b 0) p)
((even? b) (r-mul-iter (double a) (halve-floor b) p))
(else (r-mul-iter (double a) (halve-floor b) (+ p a)))))
(define (main args)
(print (mul 3 1))
(print (mul 3 2))
(print (mul 3 3))
(print (mul 3 4))
(print (russian-peasant-mul 45 113))
)