2016年8月23日火曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs(Text Editor)
  • Scheme (プログラミング言語)
  • kscheme (ksi)(github) (処理系)

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原著: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の第1章(手続きによる抽象の構築)、1.2(手続きとその生成するプロセス)、1.2.4(べき乗)、問題1.16.を取り組んでみる。

その他参考書籍

問題1.16.

コード(Emacs)

(begin
  (newline)
  (load "procedures.scm")

  (define fast-expt-1
    (lambda (b n)
      (if (= n 0)
          1
          (if (even? n)
              (square (fast-expt-1 b (/ n 2)))
              (* b (fast-expt-1 b (- n 1)))))))

  (define iter
    (lambda (b n a)
      (if (= n 0)
          a
          (if (even? n)
              (iter (square b) (/ n 2) a)
              (iter b (- n 1) (* a b))))))
              
  (define fast-expt-2
    (lambda (b n)
      (iter b n 1)))

  (define nums '(1 2 3 4 5 6 7 8 9 10))
  (for-each (lambda (b)
              (for-each (lambda (n)
                          (define x (fast-expt-1 b n))
                          (define y (fast-expt-2 b n))
                          (display "(expt ")
                          (display b)
                          (display " ")
                          (display n)
                          (display ")")
                          (newline)
                          (display x)
                          (display " ")
                          (display y)
                          (display " ")
                          (display (= x y))
                          (newline))
                        nums))
            nums)
            
'done)

入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))

$ ksi < sample16.scm
ksi> 
(expt 1 1)
1 1 #t
(expt 1 2)
1 1 #t
(expt 1 3)
1 1 #t
(expt 1 4)
1 1 #t
(expt 1 5)
1 1 #t
(expt 1 6)
1 1 #t
(expt 1 7)
1 1 #t
(expt 1 8)
1 1 #t
(expt 1 9)
1 1 #t
(expt 1 10)
1 1 #t
(expt 2 1)
2 2 #t
(expt 2 2)
4 4 #t
(expt 2 3)
8 8 #t
(expt 2 4)
16 16 #t
(expt 2 5)
32 32 #t
(expt 2 6)
64 64 #t
(expt 2 7)
128 128 #t
(expt 2 8)
256 256 #t
(expt 2 9)
512 512 #t
(expt 2 10)
1024 1024 #t
(expt 3 1)
3 3 #t
(expt 3 2)
9 9 #t
(expt 3 3)
27 27 #t
(expt 3 4)
81 81 #t
(expt 3 5)
243 243 #t
(expt 3 6)
729 729 #t
(expt 3 7)
2187 2187 #t
(expt 3 8)
6561 6561 #t
(expt 3 9)
19683 19683 #t
(expt 3 10)
59049 59049 #t
(expt 4 1)
4 4 #t
(expt 4 2)
16 16 #t
(expt 4 3)
64 64 #t
(expt 4 4)
256 256 #t
(expt 4 5)
1024 1024 #t
(expt 4 6)
4096 4096 #t
(expt 4 7)
16384 16384 #t
(expt 4 8)
65536 65536 #t
(expt 4 9)
262144 262144 #t
(expt 4 10)
1048576 1048576 #t
(expt 5 1)
5 5 #t
(expt 5 2)
25 25 #t
(expt 5 3)
125 125 #t
(expt 5 4)
625 625 #t
(expt 5 5)
3125 3125 #t
(expt 5 6)
15625 15625 #t
(expt 5 7)
78125 78125 #t
(expt 5 8)
390625 390625 #t
(expt 5 9)
1953125 1953125 #t
(expt 5 10)
9765625 9765625 #t
(expt 6 1)
6 6 #t
(expt 6 2)
36 36 #t
(expt 6 3)
216 216 #t
(expt 6 4)
1296 1296 #t
(expt 6 5)
7776 7776 #t
(expt 6 6)
46656 46656 #t
(expt 6 7)
279936 279936 #t
(expt 6 8)
1679616 1679616 #t
(expt 6 9)
10077696 10077696 #t
(expt 6 10)
60466176 60466176 #t
(expt 7 1)
7 7 #t
(expt 7 2)
49 49 #t
(expt 7 3)
343 343 #t
(expt 7 4)
2401 2401 #t
(expt 7 5)
16807 16807 #t
(expt 7 6)
117649 117649 #t
(expt 7 7)
823543 823543 #t
(expt 7 8)
5764801 5764801 #t
(expt 7 9)
40353607 40353607 #t
(expt 7 10)
282475249 282475249 #t
(expt 8 1)
8 8 #t
(expt 8 2)
64 64 #t
(expt 8 3)
512 512 #t
(expt 8 4)
4096 4096 #t
(expt 8 5)
32768 32768 #t
(expt 8 6)
262144 262144 #t
(expt 8 7)
2097152 2097152 #t
(expt 8 8)
16777216 16777216 #t
(expt 8 9)
134217728 134217728 #t
(expt 8 10)
1073741824 1073741824 #t
(expt 9 1)
9 9 #t
(expt 9 2)
81 81 #t
(expt 9 3)
729 729 #t
(expt 9 4)
6561 6561 #t
(expt 9 5)
59049 59049 #t
(expt 9 6)
531441 531441 #t
(expt 9 7)
4782969 4782969 #t
(expt 9 8)
43046721 43046721 #t
(expt 9 9)
387420489 387420489 #t
(expt 9 10)
3486784401 3486784401 #t
(expt 10 1)
10 10 #t
(expt 10 2)
100 100 #t
(expt 10 3)
1000 1000 #t
(expt 10 4)
10000 10000 #t
(expt 10 5)
100000 100000 #t
(expt 10 6)
1000000 1000000 #t
(expt 10 7)
10000000 10000000 #t
(expt 10 8)
100000000 100000000 #t
(expt 10 9)
1000000000 1000000000 #t
(expt 10 10)
10000000000 10000000000 #t
=> done
ksi> $

0 コメント:

コメントを投稿