{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Exercises (lists, loops, if/else)\n", "=================================\n", "\n", "**Exercise:**\n", "- Construct the list of the 20th first terms of the sequence $x_n = cos(n)$ where $n \\geq 0$.\n", "- In that list count how many terms are negative and how many are positive." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Write some code that displays the following multiplication table\n", "```\n", " 1 2 3 4 5 6 7 8 9 \n", " 2 4 6 8 10 12 14 16 18 \n", " 3 6 9 12 15 18 21 24 27 \n", " 4 8 12 16 20 24 28 32 36 \n", " 5 10 15 20 25 30 35 40 45 \n", " 6 12 18 24 30 36 42 48 54 \n", " 7 14 21 28 35 42 49 56 63 \n", " 8 16 24 32 40 48 56 64 72 \n", " 9 18 27 36 45 54 63 72 81 \n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exerecise:** Using a for loop (but no list) write some code that prints the first 20 terms of the sequence $x_n = sin(1/n)$ where $n \\geq 1$?." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Using a for loop (but no list) to print the first 20 terms of the sequence\n", "$$x_0 = 0.5 \\qquad \\text{and} \\qquad x_{n+1} = 3.7 \\cos(x_n).$$" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:**\n", "- Construct the list of the first 100th terms of the sequence\n", "$$\n", "z_0 = z_1 = 1 \\qquad \\text{and} \\qquad z_{n+2} = 3 z_{n+1} - z_n\n", "$$\n", "- How many terms among these are congruent to 1 mod 7?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise**:\n", "- Write soe code that displays the following figure\n", "```\n", " *\n", " **\n", " ***\n", " ****\n", " *****\n", " ******\n", "```\n", "- Write some code that displays the following figure\n", "```\n", " *\n", " **\n", " ***\n", " ****\n", " *****\n", " ******\n", " *****\n", " ****\n", " ***\n", " **\n", " *\n", "```\n", "\n", "- Write some code that displays the following figure\n", "```\n", " *\n", " ***\n", " *****\n", " *******\n", " *****\n", " ***\n", " *\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Construct the list of even numbers between 0 and 100?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Write a small program that outputs the following\n", "```\n", " 1\n", " 2 3\n", " 4 5 6\n", " 7 8 9 10\n", " 11 12 13 14\n", " 15 16 17 18 19\n", " 20 21 22 23 24 25\n", " 26 27 28 29 30 31 32\n", " 33 34 35 36 37 38 39 40\n", " 41 42 43 44 45 46 47 48 49\n", " 50 51 52 53 54 55 56 57 58 59\n", "```" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Construst a list of lists `l` so that `l[n][k]` is the binomial number $\\binom{n}{k}$. For example, if we stop at $n=3$ the list should be\n", "```python\n", "[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]\n", "```\n", "Compute this list for $n=10$." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Given the two lists\n", "```\n", "m1 = [[1,2,3,4],[3,4,5,6],[5,6,7,8]]\n", "m2 = [[1,2,1,2],[4,3,4,3],[6,5,6,5]]\n", "```\n", "- can you compute the sum of `m1` and `m2` seen as matrices?\n", "- can you compute the product of `m1` and `m2` seen as matrices? and the product of `m2` by `m1`?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** Make the list of all positive integers smaller than 100 than can be written as the sum of two cubes. (For example $35$ can be written as $35 = 2^3 + 3^3$)." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Copyright (C) 2016 Vincent Delecroix <vincent.delecroix@u-bordeaux.fr>\n", "\n", "This work is licensed under a Creative Commons Attribution-NonCommercial 4.0\n", "International License (CC BY-NC-SA 4.0). You can either read the\n", "[Creative Commons Deed](https://creativecommons.org/licenses/by-nc-sa/4.0/)\n", "(Summary) or the [Legal Code](https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode)\n", "(Full licence)." ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }