︠8c18a7b9-ae80-471d-978c-eedeec1be08f︠ ### Programming == make the machine to do a routine work ︡412db80a-688f-4762-b31f-2aacbdf96490︡ ︠4d5ad766-d427-4000-98fa-3c324a37ecdc︠ ### Python is case-sensative a=1; A ︡1da201b2-37de-456c-bbfc-abcf7508d39c︡ ︠c49a28ea-b2d1-48a8-98ad-a520e191e9f9︠ ### Python cares about line break (unlike LaTeX) a = 1 ︡f87bdcd4-4be1-4182-b770-a3694b7ee5e8︡ ︠330ba9e5-1f5d-407d-a526-c5187e217bee︠ ### Python cares about indents if "Z">"B": print "sign the contract" ### Use tab and ctrl+tab to do indents ### convention: 1 tab = 4 spaces ### On CoCalc, change your setting at “Account” and check the box of “Spaces instead of tabs”. ︡733ce7c6-9751-403a-b5d6-a96bd04eb02c︡ ︠53b849ff-e05b-4492-affb-6e63fe9e1ddf︠ ### if statement ### this code translates a percentage grade to a letter grade ### try changing the value of score and run again score = 90; if score >= 80 and score <= 100: print "A"; elif score >= 70 and score < 80: print "B"; elif score >= 60 and score < 70: print "C"; elif score >= 0 and score < 60: print "D"; else: print "Input score not valid"; ︡396ccea7-760c-4f80-b5e2-9aefa03d9ed8︡ ︠311f0390-ac1a-4f6e-8dba-63cee89a52b9︠ ### for loop ### this code prints all positive integers less than or equal to 100 ### that is a multiple of 5 or 7 for i in range(1,101): if i%5==0 or i%7==0: print i; ︡d52ad279-9197-4383-b7ed-7362341940e2︡ ︠43b16e39-94a4-4d66-b9e6-4d20145f456a︠ ### while loop ### this code is a primitive way to find ### the least common multiple of 5 and 7 i=1; while True: if i%5==0 and i%7==0: print i; break; else: i=i+1; ︡afe028b3-d9bb-4e3a-872c-189bf72866d9︡ ︠c2f401c2-f41f-43dc-9654-37b22a5ef460︠ ### Exercise: ### Print all integers from 1 to 100 ### that is a multiple of 3, 5, or 7. ### How many of them you get? (Don't count by hand...) count=0; for i in range(1,101): ??? print count; ︡18153486-596e-4f92-aef7-124c843094a2︡ ︠6877d3b5-43cb-4c42-a505-6f485fff945d︠ ### Exercise: ### Find the sum of k^3 for k=1,...,10. total=0; for ???: ??? print total ︡0f47d34b-0a10-45cc-a50e-1bd3796e2474︡ ︠6b4df72b-bf8c-47c8-a430-3221102bc7cf︠ ### Exercise: ### Count how many pairs (a,b) with gcd(a,b)==1 ### for a=1,...,10 and b=1,...,10. for a in ???: for b in ???: ??? ︡5413c436-e52a-492e-b8fd-96dedb5b51d9︡ ︠13119363-51de-4109-a48a-b33546df7d8as︠ ### How to call a submatrix? M = matrix( [[1,4,9], [4,5,6], [7,8,9]]) print M; print "---" print M[[1,2],[0,1]]; ︡e475bc18-b28d-452a-8e70-13ed54c520d8︡ ︠b37d7ef7-c3a1-4705-ae89-54762d55f7a3︠ ### Recursion: you may call a function in the definition of the function. def det(M): m,n = M.dimensions(); if m != n: return False; # determinant only works on square matrices if n == 0: return 1; # this is a vacuous definition if n == 1: return M[0,0]; total = 0; ### expand on the first row for j in range(n): rows = range(n); rows.remove(0); cols = range(n); cols.remove(j); total += (-1)^(0+j)*M[0,j]*det(M[rows,cols]); return total; print M; print M.determinant(), det(M); ︡e08c49cb-6f1d-4bfc-89a6-19ff4bc01fd5︡ ︠9f20d4a5-6626-47fd-9308-d8da5f58503di︠ %md ## Project 1 Let $a_0=3$, $a_1=2$, $a_2=14$ and $a_n-3a_{n-1}+0a_{n-2}+4a_{n-3} = 0$. Write a function to find $a_n$ through the recurrence relation. ︡1491331a-ff0e-4231-ae95-9c18acf6be1f︡{"done":true,"md":"\n## Project 1\nLet $a_0=3$, $a_1=2$, $a_2=14$ and $a_n-3a_{n-1}+0a_{n-2}+4a_{n-3} = 0$.\n\nWrite a function to find $a_n$ through the recurrence relation."} ︠e8a40d27-ed26-4b3c-81aa-4bc1b950f580s︠ def an_rec(n): ### Your answers: if n == 0: return 3; if n == 1: return 2; if n == 2: return 14; return 3*an_rec(n-1) - 4*an_rec(n-3); ### this is not an efficient algorithm, but easy to program. ︡e18a65cf-acb6-4acd-a012-746b3b6216b0︡ ︠9521f063-083a-4bb6-b448-aef313add05ci︠ %md ## Project 2 Use the answer you found in Problem 1 of the homework to write another function that compute $a_n$ directly by the formula. Then test your answers by comparing `an_rec(n)` and `an_for(n)` for $n=1,\ldots,10$. ︡bc1a6203-7757-4240-9efd-494b052c3ed8︡{"done":true,"md":"## Project 2\nUse the answer you found in Problem 1 of the homework to write another function that compute $a_n$ directly by the formula.\n\nThen test your answers by comparing `an_rec(n)` and `an_for(n)` for $n=1,\\ldots,10$."} ︠19071467-d0cd-4343-9c78-e4b15712975c︠ def an_for(n): ### You answers: return 2*(-1)^n + 2^n + n*2^n; ### test; for n in range(1,11): print n,an_rec(n),an_for(n); ︡ed0298da-427f-4ce3-a69d-271025094c6a︡ ︠6a6551c2-dba9-4378-809b-1fc31e17c0aa︠