#OK to post homework #Natalya Ter-Saakov, Jan 28, 2024, Assignment 2 read "C2.txt": #Problem 3 I #MyIfactorI(n): Inputs a positive integer n and returns a list of prime factors in weakly increasing order using ithprime MyIfactorI:=proc(n) local i,p: if isprime(n) then return [n]: fi: for i from 1 to n do p:=ithprime(i): if evalb(0=n mod p) then return [p,op(MyIfactorI(n/p))]: fi: od: end: #MyIfactorN(n): Inputs a positive integer n and returns a list of prime factors in weakly increasing order using nextprime MyIfactorN:=proc(n) local i,p: if isprime(n) then return [n]: fi: p:=1: for i from 1 to n do p:=nextprime(p): if evalb(0=n mod p) then return [p,op(MyIfactorN(n/p))]: fi: od: end: #Problem 3 II #CompareIfactor(n): Inputs n and returns a list of how long it took maple to compute ifactor followed by how long MyIfactor took CompareIfactor:=proc(n) local TI, TN, MT, tempT: tempT:=time(): MyIfactorN(n): TN:=time()-tempT: tempT:=time(): ifactor(n): MT:=time()-tempT: tempT:=time(): MyIfactorI(n): TI:=time()-tempT: [MT, TI, TN]: end: #Having run every permutation of CompareIfactor, I conclude that, due to Maple auto-memoizing, the algorithm that takes the longest is the one I run first. Maple will not allow one to restart within a procedure or file, so to actually run a comparison, one must jump through some hoops. #Specifically run the following on every procedure for your favorite n #restart: read "hw2NatalyaTer-Saakov.txt": TimeFactoring(procedure,n); #TimeFactoring(procedure, n) TimeFactoring:=proc(procedure, n) local st: st:=time(): print(procedure(n)): time()-st: end: #New un-fun fact: The same procedure on the same number runs a different amount of time. #Just like CompareIfactor, this will not actually work. #ComparePrimes ComparePrimes:=proc(n) local iT, nT, tempT, p: p:=ithprime(n-1): # this is like instantaneous tempT:=time(): ithprime(n): iT:=time()-tempT: tempT:=time(): nextprime(p): nT:=time()-tempT: [iT,nT]: end: