wget http://www.comp.nus.edu.sg/~gem1501/assignment09.html
chmod 755 *.html
Make yourself familiar with the functions you find. You should
know the following on the implimentation.
x = new Array(longlength);
Writing the variablename is better style than writing 305 since you
can later change the value assigned to this variable without having
to change other parts the program.
longprint(x);
Leading and trailing lines full of zeroes are not printed. The
decimal dot appears at the end of the corresponding line.
Command Explanation
longadd(x,y); Adding y to x x = x+y;
longsub(x,y); Substracting y from x x = x-y;
longnegate(x); Negating x x = -x;
longinit(x,i,j); Initiallize x as i/j x = i/j;
If j<1, this operation does just x = i;
longmult(x,i,j); Multiplying x with i/j x = x*i/j;
If j<1, this operation does just x = x*i;
longnull(x) Tests for 0 (x == 0)
One can use the command longnull in while loops for testing
whether a certain value has been reached. Sample statements:
longinit(x,200,345*678);
var n = 3;
while (!longnull(y))
{ longadd(x,y); longmult(y,4,n); n = n+3; }
longprint(x);
For further examples, see the routines already coded in the
source code of the Main Program after the corresponding
"document.write"-command.
EXP(i/j) = 1 + i/(j*1) + i*i/(j*j*1*2) + i*i*i/(j*j*j*1*2*3) + ...
One can compute it iteratively by an algorithm which could be written
as follows:
x = 0; y = 1; n = 1;
while (y >= 10000^{-305})
{ x = x+y; y = y*i/(j*n); n = n+1; }
where the number y becomes 0 when it falls below 10000 to the power
of -305.
More precisely, if y comes very close to 0 such that no digit of
the representative is different from it, one can stop the summation.
This algorithm is implemented as follows. Note that "!" denotes the
logical negation in Java Script programs.
longinit(x,0,0); longinit(y,1,0); n=1;
while (!longnull(y))
{ longadd(x,y); longmult(y,1,n); n=n+1; }
longprint(x);
Adapt these commands such that not E but its square is computed.
So y should be multiplied with 2/n and not with 1/n in each round.
If all implemented well, the displayed number should have 77157
as the last digits.
h = 1/(k*k+1);
Arctan(1/k) = k*(h + h^2 * 2/3 + h^3 * 2*4/(3*5) +
h^4 * 2*4*6/(3*5*7) + h^5 * 2*4*6*8/(3*5*7*9) + ...)
The implemented algorithm to compute 24*Arctan(1/8) is then the
following:
longinit(x,0,0);
longinit(u,24*8,65);
n = 3; m = 2;
while(!longnull(u))
{ longadd(x,u);
longmult(u,m,n*65);
n=n+2; m=m+2; }
longprint(x);
So x is initialized as 0 and should later contain 24*Arctan(1/8).
The variable u contains the 24 times k times the term to be added,
this multiple can be used due to the distributivity of the infinite
sum. In each round, u is multiplied with m and divided by n*(k*k+1)
where k = 8 and k*k+1 = 65.