Help:=proc(): print(`MP(A,B), IsMatrix(A) , NP2(A,B)`): print(` NRP(a,b) `): end: #IsMatrix(A): is the list of lists a legal matrix? IsMatrix:=proc(A) local i: if not type(A,list) then RETURN(false): fi: if not {seq(type(A[i],list),i=1..nops(A))}={true} then RETURN(false): fi: if nops({ seq(nops(A[i]), i=1..nops(A))})<>1 then RETURN(false): fi: true: end: #MP(A,B): inputs two matrices A and B #given as a lists of lists, and such that #nops(A[1])=nops(B) and outputs the matrix #product AB MP:=proc(A,B) local i,j,k: if not IsMatrix(A) or not IsMatrix(B) then RETURN(FAIL): fi: if nops(A[1])<>nops(B) then RETURN(FAIL): fi: [seq( [seq(add(A[i][k]*B[k][j],k=1..nops(B)),j=1..nops(B[1]))], i=1..nops(A))]: end: #NP2(a,b): naive multiplication of a and b #both assumed to be 2-digit integers NP2:=proc(a,b) local a1,a2,b1,b2,M1,M2,M3,M4: a1:=trunc(a/10): a2:=a-a1*10; b1:=trunc(b/10): b2:=b-b1*10; M1:=a1*b1: M2:=a1*b2: M3:=a2*b1: M4:=a2*b2: M1*100+(M2+M3)*10+M4: end: #NRP(a,b): RECURSIVE naive multiplication of a and b #where a and b are arbitrary POSITIVE integers. NRP:=proc(a,b) local a1,a2,b1,b2,M1,M2,M3,M4,La,Lb: if a=0 or b=0 then RETURN(0): fi: La:=trunc(log[10](a))+1: Lb:=trunc(log[10](b))+1: if La=1 and Lb=1 then RETURN(a*b): fi: La:=trunc(La/2): Lb:=trunc(Lb/2): La:=max(La,Lb): #Write a as 10^La*a1+a2 a1:=trunc(a/10^La): a2:=a-a1*10^La; b1:=trunc(b/10^La): b2:=b-b1*10^La; M1:=NRP(a1,b1): M2:=NRP(a1,b2): M3:=NRP(a2,b1): M4:=NRP(a2,b2): M1*10^(2*La)+(M2+M3)*10^La+M4: end: