programming.txt 842 B

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