Annotation of OpenXM/src/k097/debug/trip3.k, Revision 1.1.1.1
1.1 maekawa 1: /* trip3.k to test new. 1997, 4/8 */
2: class Object extends PrimitiveObject {
3: def new0() {
4: return(Object);
5: }
6: }
7:
8: class Complex extends Object {
9: local re,im;
10: def new0(a,b) {
11: this = new(); /* or this = new(super.new0()); */
12: re = a;
13: im = b;
14: return(this);
15: }
16: def void show() {
17: Print(re); Print(" + "); Println(im);
18: }
19: def void showln() {
20: this.show(); Ln();
21: }
22:
23: }
24:
25: class Complex2 extends Complex {
26: local abs;
27: def new0(a,b) {
28: this = new(super.new0(a,b));
29: abs = a*a+b*b;
30: return(this);
31: }
32: }
33:
34: class Integer extends Object {
35: local ival;
36: def new0(i) {
37: this = new();
38: ival = i;
39: return(this);
40: }
41: one = Integer.new0(1);
42: def operator sub(b) {
43: local this2,ans;
44: this2 = this;
45: ans = Integer.new0(0);
46: ans[1] = this2[1]-b[1];
47: return(ans);
48: }
49: def operator mul(b) {
50: local this2, ans;
51: this2 = this;
52: ans = Integer.new0(0);
53: ans[1] = this2[1]*b[1];
54: return(ans);
55: }
56: def isEqual(b) {
57: local this2;
58: this2 = this;
59: if (this2[1] == b[1]) return(true);
60: else return(false);
61: }
62: def factorial() {
63: local this2,tmp;
64: this2 = this;
65: if (this.isEqual(one)) return( one );
66: tmp = this2 - one;
67: return( (tmp.factorial())* this2 );
68: }
69: /* 100 の階乗は,
70: In[12]= a=Integer.new0(100);
71: In[13]=sm1(" set_timer "); a.factorial(); sm1(" set_timer ");
72: In[14]=In[15]=User time: 6.850000 seconds, System time: 0.100000 seconds
73: */
74: def void show() {
75: Print(ival);
76: }
77: def void showln() {
78: this.show(); Ln();
79: }
80: }
81:
82:
83: def factorial(a) {
84: if (a == 1) return(1);
85: return( a*factorial(a-1) );
86: }
87: /*
88: In[19]=sm1(" set_timer "); factorial(100); sm1(" set_timer ");
89: In[20]=In[21]=User time: 0.716667 seconds, System time: 0.000000 seconds
90: 10 倍遅い. On SS465.
91: sm1 の native mode でやるとどのくらいの早さか?
92: */
93:
94:
95:
96: /* Mathematica でやると,,,,
97:
98: factorial[a_]:=Module[{},
99: If[a == 1,Return[1]];
100: Return[a*factorial[a-1]]]
101:
102:
103: $RecursionLimit = Infinity
104:
105: Timing[factorial[100]]
106:
107: Out[8]= {0.1 Second, 93326215443944152681699238856266700490715968264381621468\
108:
109: > 59296389521759999322991560894146397615651828625369792082722375825118521\
110:
111: > 0916864000000000000000000000000
112:
113: 0.1 秒であった. On SS5.
114:
115: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>