c1ddb4c814 2021-03-01 1: %%%%%%%%%%%%%%%%%%%%%
c1ddb4c814 2021-03-01 2: % PROGRAMMING
c1ddb4c814 2021-03-01 3: %%%%%%%%%%%%%%%%%%%%%
c1ddb4c814 2021-03-01 4:
c1ddb4c814 2021-03-01 5: % Define number to factorize
c1ddb4c814 2021-03-01 6: x:=42;
c1ddb4c814 2021-03-01 7:
c1ddb4c814 2021-03-01 8: % Factorize x and write out each individual
c1ddb4c814 2021-03-01 9: % factor
c1ddb4c814 2021-03-01 10: factors:=factorize(fix(x))$
c1ddb4c814 2021-03-01 11: x:=0$
c1ddb4c814 2021-03-01 12: for i:=1:length(factors) do begin
c1ddb4c814 2021-03-01 13: q:=part(factors,i);
c1ddb4c814 2021-03-01 14: for j:=1:part(q,2) do begin
c1ddb4c814 2021-03-01 15: x:=x+1;
c1ddb4c814 2021-03-01 16: write "factor ", x, ": ", part(q,1);
c1ddb4c814 2021-03-01 17: end;
c1ddb4c814 2021-03-01 18: end;
c1ddb4c814 2021-03-01 19:
c1ddb4c814 2021-03-01 20: % Procedure to calculate Legendre polynomial
c1ddb4c814 2021-03-01 21: % using recursion
c1ddb4c814 2021-03-01 22: procedure p(n,x);
c1ddb4c814 2021-03-01 23: if n<0 then rederr "Invalid argument to p(n,x)"
c1ddb4c814 2021-03-01 24: else if n=0 then 1
c1ddb4c814 2021-03-01 25: else if n=1 then x
c1ddb4c814 2021-03-01 26: else ((2*n-1)*x*p(n-1,x)-(n-1)*p(n-2,x))/n$
c1ddb4c814 2021-03-01 27:
c1ddb4c814 2021-03-01 28: % Enable fancy output
c1ddb4c814 2021-03-01 29: %fancy_output$
c1ddb4c814 2021-03-01 30:
c1ddb4c814 2021-03-01 31: % Calculate p(2,w)
c1ddb4c814 2021-03-01 32: write "P(2,w) = ", p(2,w);
c1ddb4c814 2021-03-01 33:
c1ddb4c814 2021-03-01 34: % Incidentally, p(n,x) can be calculated more
c1ddb4c814 2021-03-01 35: % efficiently as follows
c1ddb4c814 2021-03-01 36: procedure p(n,x);
c1ddb4c814 2021-03-01 37: 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 38:
c1ddb4c814 2021-03-01 39: write "P(3,w) = ", p(3,w);
c1ddb4c814 2021-03-01 40:
c1ddb4c814 2021-03-01 41: end;