/*
 * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED 
 * All rights reserved.
 * 
 * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
 * non-exclusive and royalty-free license to use, copy, modify and
 * redistribute, solely for non-commercial and non-profit purposes, the
 * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
 * conditions of this Agreement. For the avoidance of doubt, you acquire
 * only a limited right to use the SOFTWARE hereunder, and FLL or any
 * third party developer retains all rights, including but not limited to
 * copyrights, in and to the SOFTWARE.
 * 
 * (1) FLL does not grant you a license in any way for commercial
 * purposes. You may use the SOFTWARE only for non-commercial and
 * non-profit purposes only, such as academic, research and internal
 * business use.
 * (2) The SOFTWARE is protected by the Copyright Law of Japan and
 * international copyright treaties. If you make copies of the SOFTWARE,
 * with or without modification, as permitted hereunder, you shall affix
 * to all such copies of the SOFTWARE the above copyright notice.
 * (3) An explicit reference to this SOFTWARE and its copyright owner
 * shall be made on your publication or presentation in any form of the
 * results obtained by use of the SOFTWARE.
 * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
 * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
 * for such modification or the source code of the modified part of the
 * SOFTWARE.
 * 
 * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
 * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
 * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
 * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
 * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
 * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
 * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
 * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
 * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
 * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
 * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
 * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
 * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
 * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
 * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
 *
 * $OpenXM: OpenXM_contrib2/asir2000/lib/const,v 1.3 2000/08/22 05:04:21 noro Exp $ 
*/
def cat(D) {
	tstart;
	for ( S = T = P = idiv(10^D,2), I = 1, J = 3; T; I++, J += 2 ) {
		P = idiv(P*I,J); T = idiv(T*I+P,J); S += T;
	}
	tstop;
	return S;
}

def e(D,N)
{
	for ( F = 1, S = 1, I = 1; I <= N; I++ ) {
		S = S*I + 1;
		F *= I;
	}
	T = red(S/F);
	return idiv(nm(T)*10^D,dn(T));
}

def at0(X,D)
{
	for ( S = T = idiv(D,X), I = 1, Y = X^2, Sgn = -1; 
		T; 
		I += 2, Sgn *= -1 ) {
		T = idiv(T*I,Y*(I+2)); S += (Sgn*T);
	}
	return S;
}
	
def pi(D)
{
	tstart; Y = 10^D; X = 16*at0(5,Y)-4*at0(239,Y); tstop;
	return X;
}

def at1(M,D) {
for (N = 1, SGN = 1, MM = M*M, A = 0, XN = idiv(D,M); 
	XN; 
	N += 2, XN = idiv(XN,MM), SGN *= -1)
		A += (SGN*idiv(XN,N));
	return A;
}

def pi1(D) {
	tstart; Y = 10^D; X = 16*at1(5,Y)-4*at1(239,Y); tstop; 
	return X;
}

def pi2(D) {
	tstart; Y = 10^D; 
	X = 48*at1(49,Y)+128*at1(57,Y)-20*at1(239,Y)+48*at1(110443,Y); 
	tstop; 
	return X;
}


def bn(N)
{
	B = newvect(N+1); C = c2(N+1);
	for ( I = 1, B[0] = 1; I <= N; I++ ) {
		for ( D = C[I+1], J = 0, S = 0; J < I; J++ )
			S += D[J]*B[J];
		B[I] = red(-S/(I+1));
	}
	return [B,C];
}

def bp(N,B,C,V)
{
	for ( I = 0, S = 0; I <= N; I++ )
		S += C[I]*B[N-I]*V^I;
	return S;
}

/*
 * sum(N) = 1^N+2^N+...+n^N
 */

def sum(N)
{
	L = bn(N+1);
	R = car(L); C = car(cdr(L));
	S = bp(N+1,R,C[N+1],n);
	return red((subst(S,n,n+1)-subst(S,n,1))/(N+1));
}

def c(N,I)
{
	for ( M = 1, J = 0; J < I; J++ )
		M *= N-J;
	return red(M/f(I));
}

def c1(N)
{
	A = newvect(N+1); B = newvect(N+1); A[0] = 1;
	for ( K = 1; K <= N; K++ ) {
		B[0] = B[K] = 1;
		for ( J = 1; J < K; J++ ) B[J] = A[J-1]+A[J];
		T = A; A = B; B = T;
	}
	return A;
}

def c2(N)
{
	A = newvect(N+1); A[0] = B = newvect(1); B[0] = 1;
	for ( K = 1; K <= N; K++ ) {
		A[K] = B = newvect(K+1); B[0] = B[K] = 1;
		for ( P = A[K-1], J = 1; J < K; J++ ) 
			B[J] = P[J-1]+P[J];
	}
	return A;
}

def f(N)
{
	for ( I = 1, M = 1; I <= N; I++ )
		M *= I;
	return M;
}

def sumd(N,M)
{
	for ( I = 1, S = 0; I <= M; I++ )
		S += I^N;
	return S;
}

#if 0
def sqrt(A,N) {
	for ( I = 0, X = 1, B = A; I < N; I++, B *= 100, X *= 10 ) {
		while ( 1 ) {
			T = idiv(idiv(B,X) + X,2);
/*
			if ((Y = T - X)== 0)
				if ( B == X^2) return (X/(10^I));
				else break;
			else if ( (Y == 1) || (Y == -1) ) break;
*/
			if ( ( (Y = T - X) == 0 ) || (Y == 1) || (Y == -1) ) break;
			X = T;
		}
	}
	return (X/(10^I));
}
#endif

def sqrt(A) {
	for ( J = 0, T = A; T >= 2^27; J++ ) {
		T = idiv(T,2^27)+1;
	}
	for ( I = 0; T >= 2; I++ ) {
		S = idiv(T,2);
		if ( T = S+S )
			T = S;
		else
			T = S+1;
	}
	X = (2^27)^idiv(J,2)*2^idiv(I,2);
	while ( 1 ) {
		if ( (Y=X^2) < A )
			X += X;
		else if ( Y == A )
			return X;
		else
			break;
	}
	while ( 1 )
		if ( (Y = X^2) <= A )
			return X;
		else
			X = idiv(A + Y,2*X);
}
end$
