> #ok to post ; > #Yifan Zhang hw1 09/08/2020 ; > #------------------------------------------------------- ; > #Q1. Teach yourself Maple using Frank Garvan's booklet ; > diff(x^3,x); 3*x^2 ; > g:= proc(x,y) > local z,i; > global v,w; > if x+y>1 then > v:= x+y: > else w:= x-y: > fi: > return (x*y); > end; g := proc (x, y) local z, i; global v, w; if 1 < x+y then v := x+y else w := x-y end if; return x*y end proc ; > g(2,3); 6 ; > v 5 ; > ; > g(-4,5); -20 ; > w -9 ; > ; > #Q2. Human Problem: Let F(n) be the set of lists using {1,2} that add-up to n. For example, F(0)={[]}, F(1)={[1]}, F(2)={[1,1],[2]}, F(3)={[1,1,1],[1,2],[2,1]}. Write down F(6). ; > #F(6) = {[1,1,1,1,1,1],[2,1,1,1,1],[1,2,1,1,1],[1,1,2,1,1],[1,1,1,2,1],[1,1,1,1,2],[2,2,1,1],[2,1,2,1],[2,1,1,2],[1,2,2,1],[1,2,1,2],[1,1,2,2],[2,2,2]} ; > #--------------------------------------------------------- ; > #Q3. ; > #1. More specifically, understand how to append a new element to a list by #the construction L->[op(L),a]. > #What Maple line converts L1:=[Mercury,Venus,Earth] to L2:=[Mercury,Venus,Earth,Mars] ? ; > #L is appended with 'a' by operation [op(L),a]; > L1 := [Mercury,Venus,Earth]; L1 := [Mercury, Venus, Earth] ; > L2 := [op(L1),Mars]; L2 := [Mercury, Venus, Earth, Mars] ; > #2. Explain, in Plain Englis, why we initialize WALKS(m,n) with either m or n or both negative to be the Empty set {} ; > #Beacuse we are required to calculate a route from (0,0) to (m,n) for both m and n are positive, so if the user enters a negative coordinate or there is a negative m or n appears in the recursive steps, there's no route by going E and N, so we set it to empty. ; > #3. Explain, in Plain Englis, why we initialize WALKS(0,0) to be the singleton set {[]} ; > #This is an initial condition for the recursive programming. Also it means if the user wants to calculate the route from (0,0) to (0,0), we will give the answer as a set {[]}. ; > #----------------------------------------------------------- ; > #Q4. Write a Maple procedure, F(n), that inputs a non-negative integer n and outputs the set of lists in {1,2} that add-up to n. Compare the output of F(6) to the one that you found above by hand. ; > F:= proc(n) local W1,W2,w1,w2: > option remember: > w1 := n; > w2 := n; > > if w1<0 or w2<0 then > RETURN({}) > fi: > > if w1=0 or w2 = 0 then > RETURN({[]}) > fi: > > W1 := F(w1-1): > W2 := F(w2-2): > {seq([op(w1),1], w1 in W1), seq([op(w2),2], w2 in W2)}: > end: > F(0); {[]} ; > F(-1); {} ; > F(2); {[2], [1, 1]} ; > F(3); {[1, 2], [2, 1], [1, 1, 1]} ; > F(6); {[2, 2, 2], [1, 1, 2, 2], [1, 2, 1, 2], [1, 2, 2, 1], [2, 1, 1, 2], [2, 1, 2, 1], [2, 2, 1, 1], [1, 1, 1, 1, 2], [1, 1, 1, 2, 1], [1, 1, 2, 1, 1], [1, 2, 1, 1, 1], [2, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1]} ; > #The output is same to my answer from Q2. ; > #---------------------------------------------------------- ; > #Q5.Write a Maple procedure, NuF(n), that inputs a non-negative integer n and outputs the NUMBERS of lists in {1,2} that add-up to n. Make sure that nops(F(n))=NuF(n) are equal for n=1..10 ; > NuF:= proc(n) local W1,W2,w1,w2: > option remember: > > > if n<0 then > RETURN (0): > fi: > > if n=0 then > RETURN(1): > fi: > > W1:= NuF(n-1); > W2:= NuF(n-2); > > W1+W2: > end: > NuF(-3); 0 ; > NuF(0); 1 ; > NuF(1);NuF(2);NuF(3);NuF(4);NuF(5);NuF(6);NuF(7);NuF(8);NuF(9);NuF(10); 1 2 3 5 8 13 21 34 55 89 ; > nops(F(1));nops(F(2));nops(F(3));nops(F(4));nops(F(5));nops(F(6));nops(F(7));nops(F(8));nops(F(9));nops(F(10)); 1 2 3 5 8 13 21 34 55 89 ; > # for n in 1 ...10, nops(F(n))== NuF(n). ; > NuF(1000); 70330367711422815821835254877183549770181269836358732742604905087154537118196933579742249494562611733487750449241765991088186363265450223647106012053374121273867339111198139373125598767690091902245245323403501 ; > #I try to run nops(F(1000)). However, this operation takes too much memory and times. Recursion only takes log n time which is much less than linear time when we deal with a large data. ; > ;