︠62d03332-1aca-406a-93ed-34bfea3c88d0s︠ 2+2 ︡a70b5cb8-ef73-4ca1-b2ce-9a532606af8a︡{"stdout":"4\n"}︡{"done":true}︡ ︠550f4038-ab7b-4809-af0b-5e353aea8fb1as︠ %auto typeset_mode(True, display=False) ︡08ce6c26-0133-4a4b-8320-03772f20e794︡{"done":true}︡ ︠d88967cb-19bd-4ee1-aef2-3279888fdfc5s︠ matrix([[1, 2], [3,4]]) ︡3dd44bd8-7dc7-4f0b-8ff6-960cb50231df︡{"html":"
$\\left(\\begin{array}{rr}\n1 & 2 \\\\\n3 & 4\n\\end{array}\\right)$
"}︡{"done":true}︡ ︠0cc0cb05-c115-4ad5-ae06-e3d2030b5a7ci︠ %html

Introductory SageMath Course – Lecture 2


We start with a recap of last time and a few exercises. After that we learn how to write simple conditional expressions, loops, functions and some advanced list handling. The worksheet end with exercises.

Recap of list creation.

Here are some examples and exercises to practice list creation learnt in the first lecture.

︡01ec6d67-296d-4761-84b2-dcf33459257a︡{"html": "

Introductory SageMath Course – Lecture 2

\r\n


\r\n

We start with a recap of last time and a few exercises. After that we learn how to write simple conditional expressions, loops, functions and some advanced list handling. The worksheet end with exercises.

\r\n\r\n

Recap of list creation.

\r\n

Here are some examples and exercises to practice list creation learnt in the first lecture.

"}︡ ︠1d6014b7-2a11-40db-be10-54afa6d4cff0︠ c # The first ten squares L = [i^2 for i in range(0, 10)] L ︡d73d0ada-a70b-488b-9872-9dadbec7750f︡{"html":"
[$0$, $1$, $4$, $9$, $16$, $25$, $36$, $49$, $64$, $81$]
"}︡{"done":true}︡ ︠421d9f80-3763-4c75-8644-4480ed683463s︠ # The first, second, ..., ninth derivative of sin(x) [diff(sin(x), x, i) for i in range(1, 10)] ︡450a06df-ff2a-4bb2-8ef0-da51743027f5︡{"html":"
[$\\cos\\left(x\\right)$, $-\\sin\\left(x\\right)$, $-\\cos\\left(x\\right)$, $\\sin\\left(x\\right)$, $\\cos\\left(x\\right)$, $-\\sin\\left(x\\right)$, $-\\cos\\left(x\\right)$, $\\sin\\left(x\\right)$, $\\cos\\left(x\\right)$]
"}︡{"done":true}︡ ︠0bb2676b-9d35-4dca-aee2-24120a56e2d6s︠ # The sum of the first 1000 primes. sum([nth_prime(i) for i in range(1, 1001)]) ︡a588b388-b68c-40d9-9ec8-04c3a88019bc︡{"html":"
$3682913$
"}︡{"done":true}︡ ︠24327b11-1828-452f-8249-ea55becbd0f9i︠ %html

Exercises.

L1. Create a list of length 20 whose kth element is $1+2+\cdots+ k$.

L2. Create a list of length 10 whose kth element is $\int_0^1  x^k dx$.

L3. Create a list of length 50 whose kth element is the numerical value of $\frac{\sqrt{k}\binom{2k}{k}}{2^{2k}}$.

Programming constructs

Loops

We can use two kinds of loops: 'for' and 'while'. The general advice is that for loops are used for repeating the same action for elements of an interval, list, set etc. In contrast, while loops should be chosen when we are repeating an action until a condition finally fails (or succeeds). We illustrate both kinds of loops with examples.

Sage (and python) uses indentation (starting lines with empty spaces, tabs) to denote where a loop ends. Note the difference between the following two examples. Also note the colon at the end of the first line.

︡5a16a7e6-81ec-4160-8810-b7f885f2fa45︡{"html": "

Exercises.

\r\n

L1. Create a list of length 20 whose kth element is $1+2+\\cdots+ k$.

\r\n

L2. Create a list of length 10 whose kth element is $\\int_0^1  x^k dx$.

\r\n

L3. Create a list of length 50 whose kth element is the numerical value of $\\frac{\\sqrt{k}\\binom{2k}{k}}{2^{2k}}$.

\r\n\r\n

Programming constructs

\r\n

Loops

\r\n

We can use two kinds of loops: 'for' and 'while'. The general advice is that for loops are used for repeating the same action for elements of an interval, list, set etc. In contrast, while loops should be chosen when we are repeating an action until a condition finally fails (or succeeds). We illustrate both kinds of loops with examples.

\r\n

Sage (and python) uses indentation (starting lines with empty spaces, tabs) to denote where a loop ends. Note the difference between the following two examples. Also note the colon at the end of the first line.

"}︡ ︠841b101b-3236-4201-8fa4-7d573dfbaa7cs︠ for i in [1..6]: gkjk jfjj hfjhj jfjf ︡f3b36528-3d18-4f31-9e42-e4cf5d15d73d︡{"stderr":"Error in lines 1-5\nTraceback (most recent call last):\n File \"/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py\", line 1013, in execute\n exec compile(block+'\\n', '', 'single') in namespace, locals\n File \"\", line 2, in \nNameError: name 'gkjk' is not defined\n"}︡{"done":true}︡ ︠914afcff-fcd8-4ebc-b298-ef958154e1c9s︠ # Note: use xrange if the loop is very long s = 0 for i in range(1,7): s = s + i print(s) ︡2c6456e7-d284-4c07-919a-96445e311301︡{"stdout":"1\n3\n6\n10\n15\n21\n"}︡{"done":true}︡ ︠111f9163-addf-4235-9bde-f97579ccbc71s︠ s = 0 for i in range(1, 7): s = s + i print(s) ︡4fb522bd-83f0-4457-95e6-47a8ae90a389︡{"stdout":"21\n"}︡{"done":true}︡ ︠22003ba9-dd87-47b1-8fbf-9fc612cf190ds︠ # You can nest loops M = matrix(4, 4) for i in range(0, 4): for j in range(0, 4): M[i, j] = i+j M ︡1128655a-bf28-4483-a604-e70421fefb6d︡{"html":"
$\\left(\\begin{array}{rrrr}\n0 & 1 & 2 & 3 \\\\\n1 & 2 & 3 & 4 \\\\\n2 & 3 & 4 & 5 \\\\\n3 & 4 & 5 & 6\n\\end{array}\\right)$
"}︡{"done":true}︡ ︠749d7425-4e25-433f-82cc-358c9149f9b3s︠ L = [4, 7, 8, 11] for x in L: print("The square of " + str(x) + " equals " + str(x^2) + ".") ︡b07d452e-ccd0-4a09-901e-bd6da8d195ce︡{"stdout":"The square of 4 equals 16.\nThe square of 7 equals 49.\nThe square of 8 equals 64.\nThe square of 11 equals 121.\n"}︡{"done":true}︡ ︠b4681a9b-a6f7-4a88-b8aa-396716294f25s︠ # Let's find the first prime larger than one million. Recall: next_prime does just this. x = 10^6 while not is_prime(x): x = x + 1 print("The first prime number following 1000000 is: " + str(x) + ".") ︡e52e1354-dd73-417f-8e94-4bdc709380e0︡{"stdout":"The first prime number following 1000000 is: 1000003.\n"}︡{"done":true}︡ ︠36de1de2-5c1e-4313-b221-3303d887cc6e︠ is_prime(1000003) ︡dfed3ec1-cae0-4502-a013-3af95ce0ef77︡︡ ︠45a5146d-b061-4ca1-930f-232dccbd488fs︠ # Let's find the first prime number p>100 s.t. p+6, p+12 and p+18 are also prime. p = next_prime(100) while not (is_prime(p+6) and is_prime(p+12) and is_prime(p+18)): p = next_prime(p) p ︡d0d93aa1-c3de-445e-b38d-03e1d116f5c4︡{"html":"
$251$
"}︡{"done":true}︡ ︠db29788d-0964-4d29-baa4-9873799be06fs︠ [is_prime(x) for x in [251, 257, 263, 269]] ︡d5b2bd16-093f-4f95-a801-2d4d584e268b︡{"html":"
[$\\mathrm{True}$, $\\mathrm{True}$, $\\mathrm{True}$, $\\mathrm{True}$]
"}︡{"done":true}︡ ︠5eb98ea9-02fc-4a69-9f7e-e83a6c5c719bi︠ %html

Conditionals.

Checking whether a condition holds can be done by using 'if'. The indented part following 'if' is only executed when the condition holds (indentation rules are similar to loops). Optionally, we can specify what should happen when the condition fails, using the keyword 'else'. There is also an 'elif' keyword, see the examples.

︡9be72f07-f6a3-4599-8572-2c6a41a28064︡{"html": "

Conditionals.

\r\n

Checking whether a condition holds can be done by using 'if'. The indented part following 'if' is only executed when the condition holds (indentation rules are similar to loops). Optionally, we can specify what should happen when the condition fails, using the keyword 'else'. There is also an 'elif' keyword, see the examples.

"}︡ ︠f28121b3-f3c0-4e79-8c08-5e7d59d2f7dfs︠ # Count primes up to 1000 (remember, prime_pi does this already). cnt = 0 for i in range(1, 1001): if is_prime(i): cnt += 1 cnt ︡9c5c0dfd-4915-480a-ac84-06262d3b9480︡{"html":"
$168$
"}︡{"done":true}︡ ︠26c86eb2-59a0-4050-91ec-8b6ad996a885s︠ for i in range(2, 10): if not is_prime(i): print(str(i) + " is composite.") elif i % 2 == 0: print(str(i) + " is an even prime. How odd!") else: print(str(i) + " is an odd prime.") ︡c4270920-2050-43fa-a43d-6a2e7ea70f99︡{"stdout":"2 is an even prime. How odd!\n3 is an odd prime.\n4 is composite.\n5 is an odd prime.\n6 is composite.\n7 is an odd prime.\n8 is composite.\n9 is composite.\n"}︡{"done":true}︡ ︠0f3e34d4-e393-4900-873b-55bd3d8b4548i︠ %html

Functions.

When you have to perform the same calculation for several different values (e.g. counting the primes up to n for several values of n), it is beneficial to make an abstraction, and make the calculation into a named function. This is done by the keyword 'def'. The result (the output) of a function is called the return value and is specified by the keyword 'return'. It is generally a good idea to avoid duplication of code in programs and use abstraction for repeating tasks.

︡416b0c43-9cc6-40c2-a168-1275621b7b90︡{"html": "

Functions.

\n

When you have to perform the same calculation for several different values (e.g. counting the primes up to n for several values of n), it is beneficial to make an abstraction, and make the calculation into a named function. This is done by the keyword 'def'. The result (the output) of a function is called the return value and is specified by the keyword 'return'. It is generally a good idea to avoid duplication of code in programs and use abstraction for repeating tasks.

"}︡ ︠4d15a595-1c24-4cf0-9ba0-70c7684a8bb4s︠ def my_prime_counter(n): cnt = 0 for i in range(1, n+1): if is_prime(i): cnt += 1 return cnt ︡8d9c6460-3610-4e34-a1a5-adb0f3ed4353︡{"done":true}︡ ︠b6af70d2-61fc-4894-9adc-2e59465ca818s︠ my_prime_counter(1000) ︡e735eb50-f568-462d-978f-ed590e135d87︡{"html":"
$168$
"}︡{"done":true}︡ ︠be7e9ed1-1696-4c8a-b780-3825c183e810s︠ [my_prime_counter(x) for x in [100, 1000, 10000]] ︡46ae3911-3810-47f4-aae1-97158b9ee902︡{"html":"
[$25$, $168$, $1229$]
"}︡{"done":true}︡ ︠35e2611c-5ca4-4861-9ddf-05067fce07eai︠ %html

Functions can also come handy when we define matrices or graphs, for example. We create a matrix whose entry in the (i, j) position is the greatest common divisor of i and j. Then we create a matrix with $M_{ij} = ij + i + j$ using a newly defined function.

︡66c23d04-06a7-417f-a6f6-e3fff7c5951b︡{"html": "

Functions can also come handy when we define matrices or graphs, for example. We create a matrix whose entry in the (i, j) position is the greatest common divisor of i and j. Then we create a matrix with $M_{ij} = ij + i + j$ using a newly defined function.

"}︡ ︠f6c71aa3-5d54-4e85-9224-4ff3c0b974a5s︠ M = matrix(5, 5, gcd) M ︡c9014871-63e3-42fe-8f6d-cc213e89f522︡{"html":"
$\\left(\\begin{array}{rrrrr}\n0 & 1 & 2 & 3 & 4 \\\\\n1 & 1 & 1 & 1 & 1 \\\\\n2 & 1 & 2 & 1 & 2 \\\\\n3 & 1 & 1 & 3 & 1 \\\\\n4 & 1 & 2 & 1 & 4\n\\end{array}\\right)$
"}︡{"done":true}︡ ︠6208f5d9-a827-4778-9f2b-8285ae8851a4s︠ def f(i, j): return i*j + i + j M = matrix(5, 5, f) M ︡1af991ed-57e0-4333-82e1-c1cfd55f1b06︡{"html":"
$\\left(\\begin{array}{rrrrr}\n0 & 1 & 2 & 3 & 4 \\\\\n1 & 3 & 5 & 7 & 9 \\\\\n2 & 5 & 8 & 11 & 14 \\\\\n3 & 7 & 11 & 15 & 19 \\\\\n4 & 9 & 14 & 19 & 24\n\\end{array}\\right)$
"}︡{"done":true}︡ ︠736e92ae-a52c-4390-8d78-464335366f72i︠ %html

We now create a graph with vertex set $\{2, 3, \ldots 9\}$. We connect two vertices if the corresponding numbers are relatively prime.

︡ef101b4e-65ea-4024-977e-fdce0285b2f7︡{"html": "

We now create a graph with vertex set $\\{2, 3, \\ldots 9\\}$. We connect two vertices if the corresponding numbers are relatively prime.

"}︡ ︠aaeee61d-776f-4a55-9509-60ee7127c55cs︠ def is_rel_prime(i, j): return gcd(i, j) == 1 G = Graph([Set([2..9]), is_rel_prime]) ︡02e7785d-08ce-49cc-a356-c5e3db568047︡{"done":true}︡ ︠1ddd70f4-88e6-4f29-90d4-32a49186d3ces︠ G.show() ︡5dd6be17-4d1a-4e69-8bab-b4312c55dbff︡{"file":{"filename":"/home/user/.sage/temp/project-9e3e1eca-35f2-42f2-b2da-e4e4e739e6dd/110/tmp_d8yQrc.svg","show":true,"text":null,"uuid":"413f71d4-eee3-4461-ae73-5572e5fed010"},"once":false}︡{"done":true}︡ ︠05f2e306-3c2b-41cf-a84e-fc3368c612cbs︠ G.chromatic_number() ︡6b6bdc56-2d74-4dc6-aad3-5b6c6181c110︡{"html":"
$4$
"}︡{"done":true}︡ ︠3d746b3c-7a8e-4306-92d1-a057c2cb3c69s︠ G.is_hamiltonian() ︡6057e375-0a3e-4f00-8638-239d0671c8b3︡{"html":"
$\\mathrm{True}$
"}︡{"done":true}︡ ︠7e8fc55e-377b-43f5-b0bd-570ee5abd101i︠ %html

One may wonder whether it is really needed to create a function just for using it once in building a graph or matrix. The answer is no: we have the possibility to use unnamed functions called lambda expressions. See the examples below.

︡a70b98d6-9d0b-457f-9631-3abb3a1c2f19︡{"html": "

One may wonder whether it is really needed to create a function just for using it once in building a graph or matrix. The answer is no: we have the possibility to use unnamed functions called lambda expressions. See the examples below.

"}︡ ︠fa97d729-70f6-44f3-83b2-0fda404dc583s︠ G = Graph([Set([1..9]), lambda i,j: (i+j)%2 == 1]) G.show() ︡6c941d18-ad2f-410b-95a0-5ad9400319af︡{"file":{"filename":"/home/user/.sage/temp/project-9e3e1eca-35f2-42f2-b2da-e4e4e739e6dd/110/tmp_bgaUGV.svg","show":true,"text":null,"uuid":"6d987293-240b-41df-b0c0-8142cc2092a7"},"once":false}︡{"done":true}︡ ︠ef824edb-e492-4b56-8b8a-c1a37fbfdd19s︠ G.is_bipartite() ︡80fa652d-d165-4f7d-8a98-7c812ba55043︡{"html":"
$\\mathrm{True}$
"}︡{"done":true}︡ ︠ba91932c-3de6-42bd-9202-a013be4d0d9fs︠ G.bipartite_sets() ︡71dcc272-1c69-45a7-80cf-c3c40264fcbf︡{"stdout":"set([1, 3, 9, 5, 7])\nset([8, 2, 4, 6])\n"}︡{"html":"
()
"}︡{"done":true}︡ ︠87fe3756-4225-40bd-a93a-67cc63c58806s︠ M = matrix(10, 10, lambda i,j: 10*i + j) M ︡aeb64cc5-d1b1-48b2-8a71-f69a19f6c087︡{"html":"
$\\left(\\begin{array}{rrrrrrrrrr}\n0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\\\\n10 & 11 & 12 & 13 & 14 & 15 & 16 & 17 & 18 & 19 \\\\\n20 & 21 & 22 & 23 & 24 & 25 & 26 & 27 & 28 & 29 \\\\\n30 & 31 & 32 & 33 & 34 & 35 & 36 & 37 & 38 & 39 \\\\\n40 & 41 & 42 & 43 & 44 & 45 & 46 & 47 & 48 & 49 \\\\\n50 & 51 & 52 & 53 & 54 & 55 & 56 & 57 & 58 & 59 \\\\\n60 & 61 & 62 & 63 & 64 & 65 & 66 & 67 & 68 & 69 \\\\\n70 & 71 & 72 & 73 & 74 & 75 & 76 & 77 & 78 & 79 \\\\\n80 & 81 & 82 & 83 & 84 & 85 & 86 & 87 & 88 & 89 \\\\\n90 & 91 & 92 & 93 & 94 & 95 & 96 & 97 & 98 & 99\n\\end{array}\\right)$
"}︡{"done":true}︡ ︠72873083-749c-470b-b2e1-e2bf0faee7b4i︠ %html

List manipulation

There are commands that help you manipulate lists without explicitely having to use loops or fucntions. We present a few examples using map, filter, zip and reduce. Using 'map', one applies a function to all elements, 'filter' selects elements satisfying a condition. Both can be simulated by list constructions.

︡f137dadb-6ab6-4635-8c8f-31b1f383f93c︡{"html": "

List manipulation

\n

There are commands that help you manipulate lists without explicitely having to use loops or fucntions. We present a few examples using map, filter, zip and reduce. Using 'map', one applies a function to all elements, 'filter' selects elements satisfying a condition. Both can be simulated by list constructions.

"}︡ ︠9c71b1d9-9074-42b4-a67f-87499c1720bes︠ L = [1..10] map(is_prime, L) ︡bbb024ec-3e15-4789-a0ad-e1c0ec6029a6︡{"html":"
[$\\mathrm{False}$, $\\mathrm{True}$, $\\mathrm{True}$, $\\mathrm{False}$, $\\mathrm{True}$, $\\mathrm{False}$, $\\mathrm{True}$, $\\mathrm{False}$, $\\mathrm{False}$, $\\mathrm{False}$]
"}︡{"done":true}︡ ︠c85836e3-3dc5-4288-889b-2ad0ebf0b9f1s︠ map(lambda i: i^2 + 3, L) ︡9c6961a8-38ce-4e01-9d1b-eaca1795a336︡{"html":"
[$4$, $7$, $12$, $19$, $28$, $39$, $52$, $67$, $84$, $103$]
"}︡{"done":true}︡ ︠d4f56916-8fc6-4be7-992e-b543af7f1649s︠ # The same as [[1..i] for i in L] map(lambda i: [1..i], L) ︡72a7a45f-a739-414c-a1d6-4985ecbc759e︡{"html":"
[[$1$], [$1$, $2$], [$1$, $2$, $3$], [$1$, $2$, $3$, $4$], [$1$, $2$, $3$, $4$, $5$], [$1$, $2$, $3$, $4$, $5$, $6$], [$1$, $2$, $3$, $4$, $5$, $6$, $7$], [$1$, $2$, $3$, $4$, $5$, $6$, $7$, $8$], [$1$, $2$, $3$, $4$, $5$, $6$, $7$, $8$, $9$], [$1$, $2$, $3$, $4$, $5$, $6$, $7$, $8$, $9$, $10$]]
"}︡{"done":true}︡ ︠fbc0a39a-1a1d-451f-96ca-d7649330cd40s︠ filter(is_prime, Set([1..40])) ︡891ecf40-3740-404f-8283-34a94cd69f85︡{"html":"
[$2$, $3$, $5$, $7$, $11$, $13$, $17$, $19$, $23$, $29$, $31$, $37$]
"}︡{"done":true}︡ ︠953a6211-6572-4f8e-ac16-676ce43431d1s︠ # The same as [i for i in [1..60] if not(i%2 == 0) and not(i%3 == 0) and not (i%5 == 0)] filter(lambda i: not(i%2 == 0) and not(i%3 == 0) and not (i%5 == 0), [1..60]) ︡3e7ba0e0-dbd4-4f0e-ab37-b5d7985e5c9e︡{"html":"
[$1$, $7$, $11$, $13$, $17$, $19$, $23$, $29$, $31$, $37$, $41$, $43$, $47$, $49$, $53$, $59$]
"}︡{"done":true}︡ ︠040f391f-1a59-4a83-adb3-29ade2f866c9s︠ L = [1..100] L ︡b9c160e8-379f-4228-a6e4-14ed15915339︡{"html":"
[$1$, $2$, $3$, $4$, $5$, $6$, $7$, $8$, $9$, $10$, $11$, $12$, $13$, $14$, $15$, $16$, $17$, $18$, $19$, $20$, $21$, $22$, $23$, $24$, $25$, $26$, $27$, $28$, $29$, $30$, $31$, $32$, $33$, $34$, $35$, $36$, $37$, $38$, $39$, $40$, $41$, $42$, $43$, $44$, $45$, $46$, $47$, $48$, $49$, $50$, $51$, $52$, $53$, $54$, $55$, $56$, $57$, $58$, $59$, $60$, $61$, $62$, $63$, $64$, $65$, $66$, $67$, $68$, $69$, $70$, $71$, $72$, $73$, $74$, $75$, $76$, $77$, $78$, $79$, $80$, $81$, $82$, $83$, $84$, $85$, $86$, $87$, $88$, $89$, $90$, $91$, $92$, $93$, $94$, $95$, $96$, $97$, $98$, $99$, $100$]
"}︡{"done":true}︡ ︠ea1a15fe-faaa-44dc-848f-0d941672d030s︠ filter(is_prime, L) ︡7ec42026-5c84-403f-9582-d29f17f3ebf6︡{"html":"
[$2$, $3$, $5$, $7$, $11$, $13$, $17$, $19$, $23$, $29$, $31$, $37$, $41$, $43$, $47$, $53$, $59$, $61$, $67$, $71$, $73$, $79$, $83$, $89$, $97$]
"}︡{"done":true}︡ ︠7f1c1d14-e730-4dea-99bb-4b1317a45fc9s︠ L ︡768c7628-9645-4f53-8386-98a25ec63d04︡{"html":"
[$1$, $2$, $3$, $4$, $5$, $6$, $7$, $8$, $9$, $10$, $11$, $12$, $13$, $14$, $15$, $16$, $17$, $18$, $19$, $20$, $21$, $22$, $23$, $24$, $25$, $26$, $27$, $28$, $29$, $30$, $31$, $32$, $33$, $34$, $35$, $36$, $37$, $38$, $39$, $40$, $41$, $42$, $43$, $44$, $45$, $46$, $47$, $48$, $49$, $50$, $51$, $52$, $53$, $54$, $55$, $56$, $57$, $58$, $59$, $60$, $61$, $62$, $63$, $64$, $65$, $66$, $67$, $68$, $69$, $70$, $71$, $72$, $73$, $74$, $75$, $76$, $77$, $78$, $79$, $80$, $81$, $82$, $83$, $84$, $85$, $86$, $87$, $88$, $89$, $90$, $91$, $92$, $93$, $94$, $95$, $96$, $97$, $98$, $99$, $100$]
"}︡{"done":true}︡ ︠29ef0d56-fffb-4490-acf3-f5b6cd94f4aa︠ ︡7eab8cf5-b422-4831-b655-90c07ba6a465︡ ︠3367c1e7-7674-4e74-a269-030a58d15ae9i︠ %html

The function 'reduce' can accumulate a calculation through a list. Summing, multplying elements of a list is an example, but for these we already have the commands 'sum' and 'prod'. An example could be the following problem: given a list of pairs, calculate the sum of pairwise products. E.g. [[3, 4], [5, 7], [1, 18]] would give 12+35+18 = 65. We show a loop-based solution and one using 'reduce'.

︡1f5bf9a3-0229-4b18-bfa5-bad85dd62b0b︡{"html": "

The function 'reduce' can accumulate a calculation through a list. Summing, multplying elements of a list is an example, but for these we already have the commands 'sum' and 'prod'. An example could be the following problem: given a list of pairs, calculate the sum of pairwise products. E.g. [[3, 4], [5, 7], [1, 18]] would give 12+35+18 = 65. We show a loop-based solution and one using 'reduce'.

"}︡ ︠50cb8a17-d84e-4720-8ffd-949101ad3363s︠ L = [[3, 4], [5, 7], [1, 18]] s = 0 for pair in L: s += prod(pair) s ︡8466e450-5499-4843-be55-1280ab477559︡{"html":"
$65$
"}︡{"done":true}︡ ︠87e01f58-7337-4890-9ac9-e6d59a1366b0s︠ reduce(lambda so_far, new_pair: so_far + prod(new_pair), L, 0) ︡2251b0d4-77b5-4b78-a4df-7e54443a0243︡{"html":"
$65$
"}︡{"done":true}︡ ︠e67bba07-49f9-4687-8bca-c31c20b3ae84s︠ zip([1, 2, 3], [10, 20, 30]) ︡17028595-06bb-4975-9270-a5f1a8866315︡{"html":"
[($1$, $10$), ($2$, $20$), ($3$, $30$)]
"}︡{"done":true}︡ ︠c1ef326c-3046-4fee-9704-4f977d886302s︠ def my_scalar_product(L1, L2): return reduce(lambda s, p: s + prod(p), zip(L1, L2), 0) my_scalar_product([3, 5, 1], [4, 7, 18]) ︡15fcf715-4f74-46d6-9972-0cbacc2e797a︡{"html":"
$65$
"}︡{"done":true}︡ ︠ea25c94b-13ce-442a-959e-0fd5dd0b2008︠ ︡9c1f26f0-fe43-4feb-b6da-557eee1c2f18︡ ︠33955eeb-0d0d-4413-ad28-37d794efef63i︠ %html

Exercises.

1. Sum up the primes up to 10000.

2. Write a function for summing up primes up to n.

3. Sum up primes of the form $17k + 1$ up to n with a function.

4. Create a function that calculates the number of positive divisors of a positive integer using its prime factorization. Look up the formula on the web. (Hint: list(factor(n)) gives the factorization in a digestible form for a computer)

5. Create a complete graph on 6 vertices.

6. Create a 4-dimensional hypercube graph.

7. Let $S = \{1, 2, 3, 4, 5\}$. Create a graph that has the two-element subsets of $S$ as vertices. Two vertices are connected if they are disjoint. Do you recognize this graph?

7.  Let $p$ be the first prime after $10^{15}$ and let $a_n$ be a reursive sequence defined by $a_0 = 7$, $a_1 = 2$ and $a_n = a_{n-1} + a_{n-2}$ for $n\geq 3$. Calculate the millionth element of the sequence modulo $p$.

7*. Let $p$ be the first prime after $10^{15}$ and let $a_n$ be a reursive sequence defined by $a_0 = 7$, $a_1 = 2$ and $a_n = a_{n-1} + a_{n-2}$ for $n\geq 3$. Calculate the 4398046511175th element of the sequence modulo $p$.

 

︡97a0deec-beff-4966-a13e-cfbe4804b246︡{"html": "

Exercises.

\n

1. Sum up the primes up to 10000.

\n

2. Write a function for summing up primes up to n.

\n

3. Sum up primes of the form $17k + 1$ up to n with a function.

\n

4. Create a function that calculates the number of positive divisors of a positive integer using its prime factorization. Look up the formula on the web. (Hint: list(factor(n)) gives the factorization in a digestible form for a computer)

\n

5. Create a complete graph on 6 vertices.

\n

6. Create a 4-dimensional hypercube graph.

\n

7. Let $S = \\{1, 2, 3, 4, 5\\}$. Create a graph that has the two-element subsets of $S$ as vertices. Two vertices are connected if they are disjoint. Do you recognize this graph?

\n

7.  Let $p$ be the first prime after $10^{15}$ and let $a_n$ be a reursive sequence defined by $a_0 = 7$, $a_1 = 2$ and $a_n = a_{n-1} + a_{n-2}$ for $n\\geq 3$. Calculate the millionth element of the sequence modulo $p$.

\n

7*. Let $p$ be the first prime after $10^{15}$ and let $a_n$ be a reursive sequence defined by $a_0 = 7$, $a_1 = 2$ and $a_n = a_{n-1} + a_{n-2}$ for $n\\geq 3$. Calculate the 4398046511175th element of the sequence modulo $p$.

\n

 

"}︡ ︠75bdb7be-7dd0-4b65-b165-6a82284d36c0s︠ memo = {} def fibo(n): if n in memo: return memo[n] elif n < 2: memo[n] = n return n else: memo[n] = fibo(n-1) + fibo(n-2) return memo[n] ︡d4e3b865-bab0-480c-882b-3ebbdf7eedef︡{"done":true}︡ ︠bf0af0c6-f6cb-46c1-ad9b-c0eb5848beacs︠ def fibvec(n): if n == 0: return [1,0] else: prev = fibvec(n-1) a = prev[0] b = prev[1] return [a+b, a] ︡ac45f4b2-706d-4e7b-9af6-35031da7c848︡{"done":true}︡ ︠5d2bf88a-0151-46a4-98f7-d389745dcddbs︠ fibvec(100) ︡e483a56f-d23c-455a-8719-8c8784cacf3c︡{"stdout":"[573147844013817084101, 354224848179261915075]\n"}︡{"done":true}︡ ︠00a222b1-cb06-4fcc-ac31-82cd02bb7b15︠