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