c1ddb4c814 2021-03-01 jeff@gridfini: %%%%%%%%%%%%%%%%%%%%%
c1ddb4c814 2021-03-01 jeff@gridfini: % PROGRAMMING
c1ddb4c814 2021-03-01 jeff@gridfini: %%%%%%%%%%%%%%%%%%%%%
c1ddb4c814 2021-03-01 jeff@gridfini:
c1ddb4c814 2021-03-01 jeff@gridfini: % Define number to factorize
c1ddb4c814 2021-03-01 jeff@gridfini: x:=42;
c1ddb4c814 2021-03-01 jeff@gridfini:
c1ddb4c814 2021-03-01 jeff@gridfini: % Factorize x and write out each individual
c1ddb4c814 2021-03-01 jeff@gridfini: % factor
c1ddb4c814 2021-03-01 jeff@gridfini: factors:=factorize(fix(x))$
c1ddb4c814 2021-03-01 jeff@gridfini: x:=0$
c1ddb4c814 2021-03-01 jeff@gridfini: for i:=1:length(factors) do begin
c1ddb4c814 2021-03-01 jeff@gridfini: q:=part(factors,i);
c1ddb4c814 2021-03-01 jeff@gridfini: for j:=1:part(q,2) do begin
c1ddb4c814 2021-03-01 jeff@gridfini: x:=x+1;
c1ddb4c814 2021-03-01 jeff@gridfini: write "factor ", x, ": ", part(q,1);
c1ddb4c814 2021-03-01 jeff@gridfini: end;
c1ddb4c814 2021-03-01 jeff@gridfini: end;
c1ddb4c814 2021-03-01 jeff@gridfini:
c1ddb4c814 2021-03-01 jeff@gridfini: % Procedure to calculate Legendre polynomial
c1ddb4c814 2021-03-01 jeff@gridfini: % using recursion
c1ddb4c814 2021-03-01 jeff@gridfini: procedure p(n,x);
c1ddb4c814 2021-03-01 jeff@gridfini: if n<0 then rederr "Invalid argument to p(n,x)"
c1ddb4c814 2021-03-01 jeff@gridfini: else if n=0 then 1
c1ddb4c814 2021-03-01 jeff@gridfini: else if n=1 then x
c1ddb4c814 2021-03-01 jeff@gridfini: else ((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))/n$
c1ddb4c814 2021-03-01 jeff@gridfini:
c1ddb4c814 2021-03-01 jeff@gridfini: % Enable fancy output
c1ddb4c814 2021-03-01 jeff@gridfini: %fancy_output$
c1ddb4c814 2021-03-01 jeff@gridfini:
c1ddb4c814 2021-03-01 jeff@gridfini: % Calculate p(2,w)
c1ddb4c814 2021-03-01 jeff@gridfini: write "P(2,w) = ", p(2,w);
c1ddb4c814 2021-03-01 jeff@gridfini:
c1ddb4c814 2021-03-01 jeff@gridfini: % Incidentally, p(n,x) can be calculated more
c1ddb4c814 2021-03-01 jeff@gridfini: % efficiently as follows
c1ddb4c814 2021-03-01 jeff@gridfini: procedure p(n,x);
c1ddb4c814 2021-03-01 jeff@gridfini: sub(y=0,df(1/(y^2-2*x*y+1)^(1/2),y,n))/(for i:=1:n product i)$
c1ddb4c814 2021-03-01 jeff@gridfini:
c1ddb4c814 2021-03-01 jeff@gridfini: write "P(3,w) = ", p(3,w);
c1ddb4c814 2021-03-01 jeff@gridfini:
c1ddb4c814 2021-03-01 jeff@gridfini: end;