開発環境
- OS X Yosemite - Apple (OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- LLVM/Clang (コンパイラ, Xcode - Apple)
コンピュータプログラミングの概念・技法・モデル(IT Architect' Archiveクラシックモダン・コンピューティング6) (IT Architects’Archive CLASSIC MODER)(セイフ・ハリディ (著)、ピーター・ヴァン・ロイ (著)、Peter Van-Roy (著)、 Seif Haridi (著)、羽永 洋 (翻訳) 、翔泳社、原書: Concepts, Techniques, and Models of Computer Programming(CTM))の第1章(プログラミング概念入門)、1.18(練習問題)、9-a, b.(記憶域(memory store))をC言語で考えてみる。
9-a, b.(記憶域(memory store))
コード(BBEdit, Emacs)
sample9_b.h
#pragma once
typedef struct list {
double head;
struct list *tail;
} list;
list* list_new(double *in);
void list_free(list *in);
void list_print(list *in);
list* list_cons(double a, list *in);
list* add_list(list *in1, list *in2);
list* shift_left(list *in);
list* shift_right(list *in);
list* fast_pascal(int n);
list* pascal(int n);
fast_pascal.c
#include "sample9_b.h"
typedef struct {
list *lists[100];
int size;
} memory_store;
memory_store s;
list* fast_pascal(int n) {
if (n > s.size) {
s.lists[n] = add_list(shift_left(fast_pascal(n - 1)),
shift_right(fast_pascal(n - 1)));
s.size = n;
}
return s.lists[n];
}
int main() {
s = (memory_store){.lists={NULL, list_new((double[]){1, NAN})},.size=1};
for (int i = 1; i <= 25; i++)
list_print(fast_pascal(i));
}
pascal.c
#include "sample9_b.h"
list* pascal(int n) {
if (n == 1) return list_new((double[]){1, NAN});
return add_list(shift_left(pascal(n - 1)), shift_right(pascal(n - 1)));
}
int main() {
for (int i = 1; i <= 25; i++)
list_print(pascal(i));
}
入出力結果(Terminal)
$ time ./fast_pascal (1) (1 1) (1 2 1) (1 3 3 1) (1 4 6 4 1) (1 5 10 10 5 1) (1 6 15 20 15 6 1) (1 7 21 35 35 21 7 1) (1 8 28 56 70 56 28 8 1) (1 9 36 84 126 126 84 36 9 1) (1 10 45 120 210 252 210 120 45 10 1) (1 11 55 165 330 462 462 330 165 55 11 1) (1 12 66 220 495 792 924 792 495 220 66 12 1) (1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1) (1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1) (1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1) (1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1) (1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1) (1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1) (1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1) (1 20 190 1140 4845 15504 38760 77520 125970 167960 184756 167960 125970 77520 38760 15504 4845 1140 190 20 1) (1 21 210 1330 5985 20349 54264 116280 203490 293930 352716 352716 293930 203490 116280 54264 20349 5985 1330 210 21 1) (1 22 231 1540 7315 26334 74613 170544 319770 497420 646646 705432 646646 497420 319770 170544 74613 26334 7315 1540 231 22 1) (1 23 253 1771 8855 33649 100947 245157 490314 817190 1144066 1352078 1352078 1144066 817190 490314 245157 100947 33649 8855 1771 253 23 1) (1 24 276 2024 10626 42504 134596 346104 735471 1307504 1961256 2496144 2704156 2496144 1961256 1307504 735471 346104 134596 42504 10626 2024 276 24 1) real 0m0.286s user 0m0.017s sys 0m0.012s $ time ./pascal (1) (1 1) (1 2 1) (1 3 3 1) (1 4 6 4 1) (1 5 10 10 5 1) (1 6 15 20 15 6 1) (1 7 21 35 35 21 7 1) (1 8 28 56 70 56 28 8 1) (1 9 36 84 126 126 84 36 9 1) (1 10 45 120 210 252 210 120 45 10 1) (1 11 55 165 330 462 462 330 165 55 11 1) (1 12 66 220 495 792 924 792 495 220 66 12 1) (1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1) (1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1) (1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1) (1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1) (1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1) (1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1) (1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1) (1 20 190 1140 4845 15504 38760 77520 125970 167960 184756 167960 125970 77520 38760 15504 4845 1140 190 20 1) (1 21 210 1330 5985 20349 54264 116280 203490 293930 352716 352716 293930 203490 116280 54264 20349 5985 1330 210 21 1) (1 22 231 1540 7315 26334 74613 170544 319770 497420 646646 705432 646646 497420 319770 170544 74613 26334 7315 1540 231 22 1) (1 23 253 1771 8855 33649 100947 245157 490314 817190 1144066 1352078 1352078 1144066 817190 490314 245157 100947 33649 8855 1771 253 23 1) (1 24 276 2024 10626 42504 134596 346104 735471 1307504 1961256 2496144 2704156 2496144 1961256 1307504 735471 346104 134596 42504 10626 2024 276 24 1) real 2m43.222s user 2m12.950s sys 0m15.773s $
0 コメント:
コメントを投稿