Annotation of OpenXM/doc/compalg/num.tex, Revision 1.1.1.1
1.1 noro 1: \chapter{$B@0?t(B}
2:
3: \section{$B@0?t$NI=8=(B}
4: $B7W;;5!$K$*$$$F$O9b!9%l%8%9%?$N%S%C%HD9(B ($B%o!<%ID9(B)$B$N?t$KBP$9$k1i;;(B
5: $B$7$+Ds6!$5$l$:(B, $B$=$l0J>e$NBg$-$5$N@0?t(B ({\bf $BB?G\D9@0?t(B} $B$"$k$$$O(B {\bf
6: bignum}) $B$KBP$9$k1i;;$O(B
7:
8: \begin{enumerate}
9: \item $B@0?t$r(B, $B$"$k@0?t(B $B$ $B$K$h$j(B $B$ $B?JI=<($9$k(B.
10: \item $B3F7e$I$&$7$N1i;;$r(B CPU $B$N@0?t1i;;5!G=$K$h$j9T$J$&(B.
11: \item $B$3$l$i$NAH9g$;$K$h$j85$N@0?t$I$&$7$N1i;;7k2L$rF@$k(B.
12: \end{enumerate}
13: $B$H$$$&7A$G9T$o$l$k(B. $B$ $B$NCM$O(B, $B%o!<%ID9$K6a$$Bg$-$5$N(B 2 $B%Y%-$H$9$k>l(B
14: $B9g$,B?$$(B. $B$3$l$O(B,
15:
16: \begin{itemize}
17: \item $B$ $B$rBg$-$/$9$k$H(B $B$ $B?JI=8=$N7e?t$,>/$J$/$G:Q$`(B
18: \item $B$ $B0J>e$N@0?t$N>e0L(B, $B2<0L$N7e$X$NJ,2r$,(B, $B%S%C%H%7%U%H1i;;$G:Q$`(B
19: \end{itemize}
20: $B$H$$$&M}M3$K$h$k(B. $B$7$+$7(B, $B$ $B$r(B, $BH>%o!<%ID9$h$jBg(B
21: $B$-$/$H$k>l9g(B, $B>h=|;;$K$*$$$F(B,
22:
23: \begin{enumerate}
24: \item
25: $BC1@:EY@0?t(B $\times$ $BC1@:EY@0?t(B $\rightarrow$ $BG\@:EY@0?t(B
26: \item
27: $BG\@:EY@0?t(B $\rightarrow$ $BC1@:EY@0?t(B $\times$ $BC1@:EY@0?t(B + $BC1@:EY@0?t(B
28: \end{enumerate}
29: $B$H$$$&Fs$D$N1i;;$,I,MW$K$J$k(B.
30:
31: $BI8=`E*$J(B C $B8@8l$K$*$$$F$O>e5-$N1i;;$ODL>oDs6!$5$l$F$*$i$:(B, $B%"%;%s%V%j(B
32: $B8@8l$K$h$j$3$l$i$r5-=R$9$kI,MW$b@8$:$k(B. $B$?$@$7(B, $B:G6a9-$/;HMQ$5$l$F$$$k(B
33: {\tt gcc} $B$G$O(B{\tt long long} $B$"$k$$$O(B {\tt unsigned long long} $B@k8@$K(B
34: $B$h$j(B64 bit $B@0?t1i;;$rMQ$$$k$3$H$,$G$-(B, $B$=$l$K$h$j(B C $B8@8l$N$_$G$3$l$i$N1i;;(B
35: $B$,2DG=$H$J$k(B. $BNc$($P(B 1. $B$N1i;;$O(B
36:
37: \begin{verbatim}
38: typedef unsigned int UL;
39: typedef unsigned long long ULL;
40: UL a,b,ch,cl;
41: ULL c;
42: ...
43: c = (ULL)a*(ULL)b;
44: ch = (UL)(c>>32);
45: cl = (UL)(c&(((ULL)1)<<32));
46: \end{verbatim}
47: $B$K$h$j(B, {\tt ch}, {\tt cl} $B$K3F!9(B 64bit $B$N>h;;7k2L$N>e0L(B, $B2<0L(B 32bit
48: $B$,3JG<$5$l$k(B. $B$7$+$7(B, {\tt gcc} $B$N(B CPU $BKh$N<BAu$N;EJ}$K$h$C$F$O(B, 32
49: bit $B>h=|;;$,>o$K4X?t8F$S=P$7$K$J$C$?$j$9$k>l9g$b$"$k$N$G(B, $B3N<B$J9bB.2=(B
50: $B$N$?$a$K$O$d$O$j%"%;%s%V%i$"$k$$$O%$%s%i%$%s%"%;%s%V%i$r;H$&$N$b;_$`$r(B
51: $BF@$J$$(B.
52:
53: $B0J2<(B, $B$ $B?JI=8=$5$l$?Fs$D$N<+A3?t(B
54: $$u=u_nB^n+u_{n-1}B^{n-1}+\cdots+u_0$$
55: $$v=v_mB^m+v_{m-1}B^{m-1}+\cdots+v_0$$
56: ($0\le u_i, v_j \le B-1$) $B$KBP$7(B, $B2C8:>h=|1i;;$r9T$&$3$H$r9M$($k(B.
57:
58: \section{$B2C;;(B}
59: $B2C;;$O(B, $BI.;;$r9T$&$h$&$K2<0L$N7e$+$i7+$j>e$,$j$H$H$b$KB-$7$F$$$1$P$h$$(B.
60: $B$7$+$7(B, $B<B:]$N7W;;5!>e$G$N<B8=$O$=$l$[$I<+L@$G$O$J$$(B. $B$ $B$H$7$F7W;;5!(B
61: $B$N%o!<%ID90lGU(B (32bit CPU $B$J$i(B $B=2^{32}$) $B$rA*$s$@>l9g(B, $B<!$N$h$&$J%"(B
62: $B%k%4%j%:%`$H$J$k(B.
63:
64: \begin{al}
65: \begin{tabbing}
66: \\
67: Input : $u=\sum_{i=0}^{n-1} u_iB^i$, $v=\sum_{i=0}^{n-1}v_iB^i$\\
68: Output : $w = u+v$\\
69: $c \leftarrow 0$\\
70: for \= $i=0$ to $n-1$\\
71: \> $t \leftarrow u_i+v_i \bmod B$\\
72: \> if \= $t < u_i$\\
73: \>\> $t \leftarrow t+c \bmod B$\\
74: \>\> $c\leftarrow 1$\\
75: \> else\\
76: \>\> $t \leftarrow t+c \bmod B$\\
77: \>\> if $t < c$ then $c \leftarrow 1$ else $c \leftarrow 0$\\
78: \> $w_i \leftarrow t$\\
79: $w_n \leftarrow c$\\
80: return $\sum_{i=0}^n w_iB^i$
81: \end{tabbing}
82: \end{al}
83: $B$3$N%"%k%4%j%:%`$K8=$l$k(B $c \leftarrow a+b \bmod B$ $B$O(B,
84: $$a+b \ge B \Rightarrow c \leftarrow a+b-B$$ $B$r0UL#$9$k(B. $B$ $B$,%o!<%ID90lGU(B
85: $B$N>l9g(B, Section \ref{cpuint} $B$G=R$Y$?$h$&$K2C;;$O(B $B$ $B$rK!$H$7$F9T$o$l(B
86: $B$k(B. $B$3$l$O(B, C $B8@8l$G$O(B,
87:
88: \begin{verbatim}
89: unsigned int a,b,c;
90: ...
91: c = a+b;
92: \end{verbatim}
93: $B$K$h$j<B9T$G$-$k(B. $B7+$j>e$,$j$O(B, $a+b \bmod B$ $B$,(B $a$ $B$^$?$O(B $b$ $B$h$j>.(B
94: $B$5$/$J$C$F$$$k$+$I$&$+$G8!=P$G$-$k$?$a(B, $B$3$3$G=R$Y$?%"%k%4%j%:%`$K$h$j(B
95: $B2C;;$,@5$7$/<B9T$G$-$k$N$G$"$k(B.
96:
97: \section{$B8:;;(B}
98: $B8:;;$b(B, $BI.;;$HF1MM$N;EJ}$G9T$&(B.
99:
100: \begin{al}
101: \begin{tabbing}
102: \\
103: Input : $u=\sum_{i=0}^{n-1} u_iB^i$, $v=\sum_{i=0}^{n-1}v_iB^i$ ($u\ge v$)\\
104: Output : $w = u-v$\\
105: $b \leftarrow 0$\\
106: for \= $i=0$ to $n-1$\\
107: \> $t \leftarrow u_i-v_i \bmod B$\\
108: \> if \= $t > u_i$\\
109: \>\> $t \leftarrow t-b$\\
110: \>\> $b\leftarrow 1$\\
111: \> else\\
112: \>\> $t \leftarrow t-b \bmod B$\\
113: (B)\>\> if $t = B-1$ then $b \leftarrow 1$ else $b \leftarrow 0$\\
114: \> $w_i \leftarrow t$\\
115: return $\sum_{i=0}^{n-1} w_iB^i$
116: \end{tabbing}
117: \end{al}
118: $B$3$N%"%k%4%j%:%`$K8=$l$k(B $c \leftarrow a-b \bmod B$ $B$O(B,
119: $$a-b < 0 \Rightarrow c \leftarrow a-b+B$$ $B$r0UL#$9$k(B. $B$3$NA`:n$b(B,
120: C $B8@8l$G$O(B
121:
122: \begin{verbatim}
123: unsigned int a,b,c;
124: ...
125: c = a-b;
126: \end{verbatim}
127: $B$K$h$j<B9T$5$l$k(B. $B7+$j2<$,$j$O(B, $a-b \bmod B$ $B$,(B $a$ $B$h$jBg$-$$$+$I$&$+(B
128: $B$G8!=P$G$-$k(B. (B) $B$K$*$1$k(B $B-1$ $B$H$NHf3S$O(B, $B$3$3$G$N7+$j2<$,$j$,(B, $t=0$
129: $B$+$D(B $b=1$ $B$N>l9g$K$N$_@8$:$k$?$a$G$"$k(B.
130:
131: \section{$B>h;;(B}
132:
133: $B>h;;$bI.;;$HF1MM$NJ}K!$G7W;;$G$-$k(B.
134:
135: \begin{al}
136: \begin{tabbing}
137: \\
138: Input : $u=\sum_{i=0}^{n-1} u_iB^i$, $v=\sum_{i=0}^{m-1}v_iB^i$\\
139: Output : $w = uv$\\
140: for \= $i=0$ to $n-1$\\
141: \> $w_i \leftarrow 0$\\
142: for $j=0$ to $m-1$\\
143: \>$c \leftarrow 0$\\
144: \>for \= $i=0$ to $n-1$\\
145: \>\> $t \leftarrow w_{i+j}+u_iv_j+c$\\
146: (M)\>\> $t = t_hB+t_l$ ($0 \le t_h, t_l < B$) $B$HJ,2r(B\\
147: \>\> $w_{i+j}\leftarrow t_l$\\
148: \>\> $c\leftarrow t_h$\\
149: \>$w_{n+j} \leftarrow c$\\
150: \end{tabbing}
151: \end{al}
152: (M) $B$G7W;;$5$l$k(B $t$ $B$O(B, $0\le, w_{i+j}, c<B$ ($B$ $B?J(B 1 $B7e(B) $B$N85$h$j(B
153: $$ t=w_{i+j}+u_iv_j+c \le (B-1)+(B-1)^2+B-1 = B^2-1$$$B$h$j(B $B$ $B?J$G9b!9(B
154: 2 $B7e$N?t$G$"$k(B. $B$h$C$F(B $t_h$, $t_l$ $B$O(B $B$ $B?J(B 1 $B7e$H$J$j(B, $B5"G<E*$K(B,
155: $w_{i+j}$, $c$ $B$,>o$K(B $B$ $B?J(B 1 $B7e$H$J$k$3$H$,J,$+$k(B. $B$?$@$7(B, $u_iv_j$
156: $B$N7W;;$K$OG\@:EY@0?t>h;;$,I,MW$H$J$k(B.
157:
158: \section{$B=|;;(B}
159: $B@0?t=|;;$b4pK\$OI.;;$HF1MM$G(B, $B>&$N>e0L$N7e$+$i5a$a$F$$$/(B.
160: $B4JC1$N$?$a(B, $BHo=|?t(B, $B=|?t$r$=$l$>$l(B
161: $$u = u_{n+1}B^{n+1}+\cdots+u_0$$
162: $$v = v_n B^n+\cdots+v_0$$ ($0\le u_i,v_i < B$, $u/v < B$) $B$H$9$k(B. $BLd(B
163: $BBj$O(B, $B>&$rG!2?$K@53N$K8+@Q$b$k$3$H$,$G$-$k$+$G$"$k(B. $B<!$K>R2p$9$k$N$O(B
164: Knuth \cite[Section 4.3]{KNUTH} $B$K=R$Y$i$l$F$$$k$b$N$G(B, $B>&$N8uJd$r(B, $B=|?t$N>e0L(B 2 $B7e(B,
165: $BHo=|?t$N>e0L(B 3 $B7e$+$i5a$a$k(B.
166: $\lfloor a \rfloor$ $B$O(B, $a$ $B$r1[$($J$$:GBg$N@0?t$H$9$k(B.
167: $q = \lfloor u/v \rfloor$ $B$G$"$k(B.
168:
169: \begin{pr}
170: \label{approx}
171: $\hat{q} = \min(B-1,\lfloor (u_{n+1}B+u_n)/v_n \rfloor)$ $B$H$9$k(B.
172: $B$3$N;~(B, $v_n \ge \lfloor B/2 \rfloor \Rightarrow q \le \hat{q} \le q+2.$
173: \end{pr}
174: $BL?Bj(B \ref{approx} $B$K$h$j(B, $v_n \ge \lfloor B/2 \rfloor$ $B$N85$G(B, $B??$NCM(B
175: $q$ $B$h$j9b!9(B 2 $B$@$1Bg$-$$6a;wCM(B $\hat{q}$ $B$,F@$i$l$k(B.
176:
177: \begin{pr}
178: \label{check}
179: $\hat{q} \ge q$ $B$+$D(B
180: $\hat{q}(v_n B + v_{n-1}) > u_{n+1}B^2+u_n B + u_{n-1} \Rightarrow q \le \hat{q}-1.$
181: \end{pr}
182: $\hat{q} = q+2$ $B$J$i$P>o$KL?Bj(B \ref{check} $B$NITEy<0$,@.N)$9$k$+$i(B, $B$3(B
183: $B$N%A%'%C%/$r9b!9(B 2 $B2s9T$J$&$3$H$G(B,
184: $\hat{q} = q$ $B$^$?$O(B $\hat{q}=q+1$
185: $B$H$G$-$k(B. $B$3$N%A%'%C%/$r9T$C$?$N$A(B, $B$J$*(B $\hat{q} \neq q$ $B$H$J$k(B
186: $B2DG=@-$K$D$$$F$O<!$NL?Bj$,@.$jN)$D(B.
187:
188: \begin{pr}
189: $\hat{q} > q$ $B$+$D(B
190: $\hat{q}(v_n B + v_{n-1}) \le u_{n+1}B^2+u_n B + u_{n-1}$
191: $\Rightarrow u \bmod v \ge (1-2/B)v.$
192: \end{pr}
193: $B$9$J$o$A(B, $B%i%s%@%`$K(B $u$, $v$ $B$rA*$s$@$H$-(B, $\hat{q} \neq q$ $B$H$J$k(B
194: $B3NN($O(B $2/B$ $BDxEY$H$J$j(B, $B=2^{32}$ $B$N>l9g$K$O6K$a$F>.$5$$3NN($G$7$+(B
195: $B5/$3$i$J$$$3$H$,J,$+$k(B.
196: $B$3$N(B $\hat{q}$ $B$K$h$j(B,
197: $\hat{r} = u-\hat{q}v$ $B$r<B:]$K7W;;$7(B
198: $B$F(B, $B$b$7(B $\hat{r}$ $B$,Ii$J$i$P(B $q = \hat{q}-1$ $B$G$"$j(B,
199: $r = u-qv = \hat{r}+v $
200: $B$H$J$k(B.
201: $BL?Bj(B \ref{check} $B$NITEy<0$O(B
202: $$\hat{q}v_{n-1} > (u_{n+1}B+u_n-\hat{q}v_n)B+u_{n-1}$$
203: $B$H=q$1$k$+$i(B, $B$3$N%A%'%C%/$GI,MW(B
204: $B$K$J$k@0?t1i;;$O9b!9G\@:EY$G==J,$G$"$k(B. $B$^$H$a$k$H(B,
205:
206: \begin{itemize}
207: \item $\hat{q} = q$ $B$^$?$O(B $\hat{q} = q + 1$$B$G(B, $B8e<T$H$J$k3NN($O6K$a$F>.$5$$(B.
208: \item $B@0?t>h=|;;$OG\@:EY>h=|;;$G==J,$G$"$k(B.
209: \end{itemize}
210: $BA0<T$O(B, $B<B:]$KB-$7La$7$,I,MW$K$J$k>l9g$,6K$a$F>.$5$$$3$H$r0UL#$7(B, $B=|;;(B
211: $B$N8zN(8~>e$K$D$J$,$k$,(B, $BH?LL(B, $BB-$7La$7$r9T$J$&ItJ,$N%P%0$H$j$K6lO+$9$k(B
212: $B$3$H$J$k(B. $B$^$?(B, $B8e<T$O(B, $BG\@:EY@0?t>h=|;;$5$(<B8=$5$l$F$$$l$P(B, $B$3$N%"%k(B
213: $B%4%j%:%`$O<B8=2DG=$G$"$k$3$H$r<($7$F$$$k(B.
214: $B>e5-$NJ}K!$G(B, $v_n \ge \lfloor B/2 \rfloor$ $B$J$k@)8B$,M?$($i$l$F$$$k$,(B,
215: $B$3$l$O(B, $B$"$i$+$8$a(B $u, v$ $B$K(B $d=\lfloor B/(v_n+1)\rfloor$ $B$r3]$1$F$*$1$P(B
216: $BK~$?$5$l$k(B. $B>&$OJQ2=$;$:(B, $B>jM>$O(B $d$ $B$G3d$l$P$h$$(B. $B$^$?(B, $B$ $B$,(B 2 $B$NQQ(B
217: $B$N>l9g$K$O(B, $d$ $B$HCM$H$7$F(B $BE,Ev$J(B 2 $B$NQQ$,$H$l$k(B. $B$3$N>l9g(B, $d$ $B$K$h$k(B
218: $B>h=|;;$O(B, $B%7%U%H1i;;$H$J$jET9g$,$h$$(B.
219:
220: \section{$BQQ(B}
221:
222: $B@0?t$NQQ>h$N7W;;$O(B, $B<!$N$h$&$J(B 2 $BDL$j$N%"%k%4%j%:%`$G9T$&$3$H$,$G$-$k(B.
223:
224: \begin{al}
225: \label{ipwr0}
226: \begin{tabbing}
227: \\
228: Input : $u \in \Z$, $e \in \N$\\
229: Output : $w = u^e$\\
230: $(k_mk_{m-1}\cdots k_0)_2 \leftarrow e$ $B$N(B 2 $B?JI=<((B ($k_m=1$)\\
231: $t \leftarrow u$\\
232: $w \leftarrow 1$\\
233: for \= $i=0$ to $m$\{\\
234: (a)\> if ( $k_i=1$ ) then $w = wt$\\
235: \> if ( $i=m$ ) then return $w$\\
236: \> $t \leftarrow t^2$\\
237: \}
238: \end{tabbing}
239: \end{al}
240:
241: \begin{pr}
242: $B%"%k%4%j%:%`(B \ref{ipwr0} $B$O(B $u^e$ $B$r=PNO$9$k(B.
243: \end{pr}
244: \proof (a) $B$K$*$$$F(B, $t=u^{2^i}$ $B$h$j(B, $B$3$N%"%k%4%j%:%`$O(B, $k_i=1$
245: $B$J$k(B $i$ $B$KBP$9$k(B $u^{2^i}$ $B$N@Q$r=PNO$9$k(B. $B$3$NCM$O(B $u^e$ $B$KEy$7$$(B. \qed
246:
247: \begin{al}
248: \label{ipwr1}
249: \begin{tabbing}
250: \\
251: Input : $u \in \Z$, $e \in \N$\\
252: Output : $w = u^e$\\
253: $(k_mk_{m-1}\cdots k_0)_2 \leftarrow e$ $B$N(B 2 $B?JI=<((B ($k_m=1$)\\
254: $w \leftarrow 1$\\
255: for \= $i=m$ to $0$\{\\
256: (a)\> $w \leftarrow w^2$\\
257: \> if ( $k_i=1$ ) then $w = wu$\\
258: \}\\
259: return $w$
260: \end{tabbing}
261: \end{al}
262:
263: \begin{pr}
264: $B%"%k%4%j%:%`(B \ref{ipwr1} $B$O(B $u^e$ $B$r=PNO$9$k(B.
265: \end{pr}
266: \proof $m=0$ $B$N;~(B $w=u$ $B$H$J$j@5$7$$(B. $m-1$ $B$^$G@5$7$$$H$9$k(B.
267: $e_1 = (k_mk_{m-1}\cdots k_1)$ $B$r(B $m$ bit $B?t$H$_$J$;$P(B, $B5"G<K!$N(B
268: $B2>Dj$K$h$j(B, $i=0$ $B$G(B (a) $B$K$*$$$F(B $w=u^{e_1}$.
269: $B$3$N$H$-(B, $B$3$N%"%k%4%j%:%`$O(B $w^2\cdot u^{k_0}=u^{2e_1+k_0}$
270: $B$r=PNO$9$k$,(B, $B$3$l$O(B $u^e$ $B$KEy$7$$(B. \qed\\
271: $B$3$l$i$N%"%k%4%j%:%`$O(B, $e$ $B>h$r(B, $B$=$l$>$l(B $\log_2 e$ $B2sDxEY$N(B 2 $B>h;;$H(B
272: $B>h;;$G<B9T$G$-$k(B. $B$3$N%"%k%4%j%:%`$O(B, $B@0?t$NQQ>h$K8B$i$:(B, $B$"$i$f$k(B
273: $B>l9g$KMQ$$$i$l$kHFMQE*$J$b$N$G$"$k(B. $BA0<T$N%"%k%4%j%:%`$O(B,
274: \begin{itemize}
275: \item $B;X?t(B $e$ $B$N3F%S%C%H$r1&$+$i:8$K%9%-%c%s$9$k(B.
276: \item $B>o$K3]$1$k?t$OF1$8(B.
277: \item $BJ];}$9$Y$-ESCf7k2L$O(B $w$ $B$N$_(B.
278: \end{itemize}
279: $B8e<T$O(B
280: \begin{itemize}
281: \item $B;X?t(B $e$ $B$N3F%S%C%H$r:8$+$i1&$K%9%-%c%s$9$k(B.
282: \item $B3]$1$k?t$,JQ2=$7$F$$$/(B.
283: \item $BJ];}$9$Y$-ESCf7k2L$O(B $w$, $t$ $B$NN>J}(B.
284: \end{itemize}
285: $B$H$$$&FCD'$r;}$A(B, $B$=$l$>$l0lD90lC;$,$"$k(B.
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>