{ "metadata": { "name": "ch4" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "heading", "level": 1, "metadata": {}, "source": [ "Chapter 4: Iteration" ] }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.1, page no. 60" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i=1\n", "print \"Enter a positive integer: \"\n", "n = int(raw_input())\n", "s=0\n", "while (i <= n):\n", " s += i\n", " i += 1\n", "print \"The sum of the first %d integers is %d\" %(i,s)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The sum of the first 6 integers is 15\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.2, page no. 61" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter a positive integer: \"\n", "bound = int(raw_input())\n", "s=0.0\n", "i=0\n", "while (s < bound):\n", " i += 1\n", " s += 1.0/i\n", "\n", "print \"The sum of the first %d reciprocals is %f\" %(i,s)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The sum of the first 83 reciprocals is 5.002068\n" ] } ], "prompt_number": 2 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.3, page no. 61" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "print \"Enter a positive number: \"\n", "x = float(raw_input())\n", "while (x > 0):\n", " print \"sqrt(%d) = %f \"%(x,math.sqrt(x))\n", " print \"Enter another positive number (or 0 to quit): \"\n", " x = float(raw_input())\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive number: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "sqrt(5) = 2.236068 \n", "Enter another positive number (or 0 to quit): \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "3\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "sqrt(3) = 1.732051 \n", "Enter another positive number (or 0 to quit): \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.4, page no. 62" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i=1\n", "print \"Enter a positive integer: \";\n", "n = int(raw_input())\n", "s=0\n", "while(True):\n", " if (i > n):\n", " break # terminates the loop immediately\n", " s += i\n", " i += 1\n", "print \"The sum of the first %d integers is %d\" %(n,s)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The sum of the first 5 integers is 15\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.5, page no. 62\n" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter a positive integer: \"\n", "bound = int(raw_input())\n", "print \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\n", "f0=0\n", "f1=1\n", "while (True):\n", " f2 = f0 + f1\n", " if (f2 > bound):\n", " break\n", " print \", %d\" % f2,\n", " f0 = f1\n", " f1 = f2\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Fibonacci numbers < 10:\n", "0, 1 , 1 , 2 , 3 , 5 , 8\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.6, page no. 63" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import sys\n", "print \"Enter a positive integer: \"\n", "bound = int(raw_input())\n", "print \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\n", "f0=0\n", "f1=1\n", "while (True):\n", " f2 = f0 + f1\n", " if (f2 > bound):\n", " sys.exit(0)\n", " print \", %d\" % f2,\n", " f0 = f1\n", " f1 = f2\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] }, { "ename": "SystemExit", "evalue": "0", "output_type": "pyerr", "traceback": [ "An exception has occurred, use %tb to see the full traceback.\n", "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Fibonacci numbers < 10:\n", "0, 1 , 1 , 2 , 3 , 5 , 8" ] }, { "output_type": "stream", "stream": "stderr", "text": [ "To exit: use 'exit', 'quit', or Ctrl-D.\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.7, page no. 63" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter a positive integer: \"\n", "bound = int(raw_input())\n", "print \"Fibonacci numbers < %d:\\n0, 1\" % bound ,\n", "f0=0\n", "f1=1\n", "# Error : infinite loop !\n", "while (True):\n", " f2 = f0 + f1\n", " # By commenting the below if statement, it goes to infinite.\n", " if (f2 > bound):\n", " break\n", " print \", %d\" % f2,\n", " f0 = f1\n", " f1 = f2" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n", "Fibonacci numbers < 10:\n", "0, 1 , 1 , 2 , 3 , 5 , 8\n" ] } ], "prompt_number": 1 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.8, page no. 64" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i=0\n", "print \"Enter a positive integer: \"\n", "n = int(raw_input())\n", "s=0\n", "while i<=n:\n", " s += i\n", " i += 1\n", "print \"The sum of the first %d integers is %d\" %(n,s)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The sum of the first 10 integers is 55\n" ] } ], "prompt_number": 3 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.9, page no. 65" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter a positive integer: \"\n", "bound = int(raw_input())\n", "print \"Factorial numbers < %d:\\n1, 1\" %bound,\n", "f=1\n", "i=1\n", "while f < bound:\n", " i += 1\n", " f *= i\n", " print \", %d\" %f,\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Factorial numbers < 10:\n", "1, 1 , 2 , 6 , 24\n" ] } ], "prompt_number": 4 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.10, page no. 66" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter a positive integer: \"\n", "n = int(raw_input())\n", "s=0;\n", "for i in range(0,n+1):\n", " s += i\n", "print \"The sum of the first %d integers is %d\" %(n,s)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The sum of the first 10 integers is 55\n" ] } ], "prompt_number": 5 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.11, page no. 66" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter a positive integer: \"\n", "n = int(raw_input())\n", "s=0\n", "for i in range(1,n/2): # the scope of this i is this loop\n", " s += i\n", "\n", "for i in range(n/2,n+1): # the scope of this i is this loop\n", " s += i\n", "print \"The sum of the first %d integers is %d\" % (n,s)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The sum of the first 10 integers is 55\n" ] } ], "prompt_number": 6 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.12, page no. 66" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter a positive integer: \"\n", "bound = int(raw_input())\n", "\n", "print \"Factorial numbers that are <= %d:\\n1, 1\" %bound,\n", "f=1\n", "for i in range(2,bound+1):\n", " f *= i\n", " print \", %d\" % f,\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "Factorial numbers that are <= 10:\n", "1, 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 , 3628800\n" ] } ], "prompt_number": 7 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.13, page no. 67" ] }, { "cell_type": "code", "collapsed": false, "input": [ "\n", "\n", "for i in range(10,0,-1):\n", " print i,\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "10 9 8 7 6 5 4 3 2 1\n" ] } ], "prompt_number": 8 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.14, page no. 67" ] }, { "cell_type": "code", "collapsed": false, "input": [ "prime = True\n", "print \"Enter a positive integer: \"\n", "n = int(raw_input())\n", "if (n < 2):\n", " print \"%d is not prime.\" %n\n", " prime = False\n", "elif (n < 4):\n", " print \"%d is prime.\" %n\n", " prime = False\n", "elif (n%2 == 0):\n", " print \"%d = 2* %d\" %(n,n/2)\n", " prime = False\n", "else:\n", " for d in range(3,n/2+1):\n", " if (n%d == 0):\n", " print \"%d = %d * %d\" %(n,d,n/d)\n", " prime = False\n", "if prime: \n", " print \"%d is prime.\"%n\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "11\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "11 is prime.\n" ] } ], "prompt_number": 9 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.15, page no. 68" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter positive integers (0 to quit): \";\n", "n = int(raw_input())\n", "m = n\n", "while n > 0:\n", " n = int(raw_input())\n", " if n > m :\n", " m = n\n", "\n", "print \"max = %d\" % m" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter positive integers (0 to quit): \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "19\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "42\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "max = 42\n" ] } ], "prompt_number": 10 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.16, page no. 68" ] }, { "cell_type": "code", "collapsed": false, "input": [ "print \"Enter positive integers (0 to quit): \";\n", "n = int(raw_input())\n", "m = n\n", "while n > 0: \n", " if n < m :\n", " m = n\n", " n = int(raw_input())\n", "\n", "print \"min = %d\" % m\n", "\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter positive integers (0 to quit): \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "19\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "42\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "1\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "min = 1\n" ] } ], "prompt_number": 11 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.17, page no. 69" ] }, { "cell_type": "code", "collapsed": false, "input": [ "m = 95\n", "n = 11\n", "while m%n > 0:\n", " print \"%d modulo %d = %d\" %(m,n,m%n)\n", " m -= 3\n", " n += 1\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "95 modulo 11 = 7\n", "92 modulo 12 = 8\n", "89 modulo 13 = 11\n", "86 modulo 14 = 2\n", "83 modulo 15 = 8\n" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.18, page no. 69" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(1,13):\n", " for y in range(1,13):\n", " print \"%4d\" % (x*y),\n", " print \"\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 1 2 3 4 5 6 7 8 9 10 11 12 \n", " 2 4 6 8 10 12 14 16 18 20 22 24 \n", " 3 6 9 12 15 18 21 24 27 30 33 36 \n", " 4 8 12 16 20 24 28 32 36 40 44 48 \n", " 5 10 15 20 25 30 35 40 45 50 55 60 \n", " 6 12 18 24 30 36 42 48 54 60 66 72 \n", " 7 14 21 28 35 42 49 56 63 70 77 84 \n", " 8 16 24 32 40 48 56 64 72 80 88 96 \n", " 9 18 27 36 45 54 63 72 81 90 99 108 \n", " 10 20 30 40 50 60 70 80 90 100 110 120 \n", " 11 22 33 44 55 66 77 88 99 110 121 132 \n", " 12 24 36 48 60 72 84 96 108 120 132 144 \n" ] } ], "prompt_number": 13 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.19, page no. 70" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import math\n", "# defines pow() and log()\n", "\n", "print \"Enter a positive integer: \"\n", "n = int(raw_input())\n", "d=0 # the discrete binary logarithm of n\n", "p2d=1 # = 2^d\n", "i = n\n", "while i > 1:\n", " # INVARIANT: 2^d <= n/i < 2*2^d\n", " p2d=math.pow(2,d) # = 2^d\n", " print \"%2d <= %2d\" %(p2d,2*p2d)\n", " i /= 2\n", " d += 1\n", "\n", "p2d=math.pow(2,d) # = 2^d\n", "print \"%2d <= %2d < %2d\" %(p2d,n,2*p2d)\n", "print \" The discrete binary logarithm of is %d\" % d \n", "lgn = math.log(n)/math.log(2) # base 2 logarithm\n", "print \"The continuous binary logarithm of is %f\" % lgn" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "17\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " 1 <= 2\n", " 2 <= 4\n", " 4 <= 8\n", " 8 <= 16\n", "16 <= 17 < 32\n", " The discrete binary logarithm of is 4\n", "The continuous binary logarithm of is 4.087463\n" ] } ], "prompt_number": 14 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.20, page no. 71" ] }, { "cell_type": "code", "collapsed": false, "input": [ "i=1\n", "print \"Enter a positive integer: \"\n", "n = int(raw_input())\n", "s=0\n", "while (True):\n", " if (i > n):\n", " break\n", " s += i\n", " i += 1\n", "\n", "print \"The sum of the first %d integers is %d\" %(i,s)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter a positive integer: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "10\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "The sum of the first 11 integers is 55\n" ] } ], "prompt_number": 15 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.21, page no. 71" ] }, { "cell_type": "code", "collapsed": false, "input": [ "count=0\n", "s=0\n", "print \"Enter positive integers (0 to quit):\" \n", "while True: # \"forever\"\n", " print \"\\t %d :\" %(count + 1),\n", " n = int(raw_input())\n", " if (n <= 0):\n", " break\n", " count += 1\n", " s += n\n", "\n", "print \"The average of those %d positive numbers is \" %count,\n", "print float(s)/count\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter positive integers (0 to quit):\n", "\t 1 :" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "12\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \t 2 :" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "32\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \t 3 :" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "11\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " \t 4 :" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "0\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ " The average of those 3 positive numbers is 18.3333333333\n" ] } ], "prompt_number": 16 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.22, page no. 72" ] }, { "cell_type": "code", "collapsed": false, "input": [ "for x in range(1,13):\n", " for y in range(1,13):\n", " if y>x:\n", " break\n", " else:\n", " print '%4d' %(x*y),\n", " print ''\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ " 1 \n", " 2 4 \n", " 3 6 9 \n", " 4 8 12 16 \n", " 5 10 15 20 25 \n", " 6 12 18 24 30 36 \n", " 7 14 21 28 35 42 49 \n", " 8 16 24 32 40 48 56 64 \n", " 9 18 27 36 45 54 63 72 81 \n", " 10 20 30 40 50 60 70 80 90 100 \n", " 11 22 33 44 55 66 77 88 99 110 121 \n", " 12 24 36 48 60 72 84 96 108 120 132 144 \n" ] } ], "prompt_number": 17 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.23, page no. 73" ] }, { "cell_type": "code", "collapsed": false, "input": [ "while True:\n", " n = int(raw_input('Enter int : '))\n", " if (n%2 == 0):\n", " continue\n", " if (n%3 == 0):\n", " break\n", " print \"\\tBottom of loop.\\n\"\n", "print \"\\tOutside of loop.\\n\"\n" ], "language": "python", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter int : 5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tBottom of loop.\n", "\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter int : 4\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter int : 6\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "Enter int : 9\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\tOutside of loop.\n", "\n" ] } ], "prompt_number": 18 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.24, page no. 74" ] }, { "cell_type": "code", "collapsed": false, "input": [ "N=5\n", "done=False\n", "for i in range(N):\n", " for j in range(N):\n", " if done:\n", " break\n", " for k in range(N):\n", " if done:\n", " break\n", " if (i+j+k>N):\n", " done = True\n", " else:\n", " print i+j+k,\n", " print \" \",\n", " print \"* \"\n", " print \".\" \n", " done = False\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1 2 3 4 * \n", "1 2 3 4 5 * \n", "2 3 4 5 * \n", ".\n", "1 2 3 4 5 * \n", "2 3 4 5 * \n", ".\n", "2 3 4 5 * \n", ".\n", "3 4 5 * \n", ".\n", "4 5 * \n", ".\n" ] } ], "prompt_number": 19 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.25, page no. 75" ] }, { "cell_type": "code", "collapsed": false, "input": [ "N=5\n", "done=False\n", "for i in range(N):\n", " for j in range(N):\n", " if done:\n", " break\n", " for k in range(N):\n", " if done:\n", " break\n", " if (i+j+k>N):\n", " done = True\n", " else:\n", " print i+j+k,\n", " print \" \",\n", " print \"* \"\n", " print \".\" \n", " done = False\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0 1 2 3 4 * \n", "1 2 3 4 5 * \n", "2 3 4 5 * \n", ".\n", "1 2 3 4 5 * \n", "2 3 4 5 * \n", ".\n", "2 3 4 5 * \n", ".\n", "3 4 5 * \n", ".\n", "4 5 * \n", ".\n" ] } ], "prompt_number": 20 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.26, page no. 76" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "\n", "# prints pseudo-random numbers:\n", "\n", "for i in range(0,8):\n", " print random.random()\n", "\n", " " ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0.702115758628\n", "0.969460447904\n", "0.409934401112\n", "0.700339443791\n", "0.093528851602\n", "0.132172955687\n", "0.0162887279366\n", "0.943010713478\n" ] } ], "prompt_number": 21 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.27, page no. 77" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "# prints pseudo-random numbers:\n", "print \"Enter seed: \"\n", "seed = int(raw_input())\n", "random.seed(seed);\n", "for i in range(0,8):\n", " print random.random()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter seed: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "0.62290169489\n", "0.741786989261\n", "0.795193565566\n", "0.942450283777\n", "0.73989857474\n", "0.922324996665\n", "0.0290052282836\n", "0.465622654378\n" ] } ], "prompt_number": 22 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.28, page no. 78" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "for i in range(0,8):\n", " print random.random()\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "0.943356716998\n", "0.648974553137\n", "0.900900491751\n", "0.113205964653\n", "0.469069047782\n", "0.24657283262\n", "0.543760859236\n", "0.573941187928\n" ] } ], "prompt_number": 23 }, { "cell_type": "heading", "level": 2, "metadata": {}, "source": [ "Example 4.29, page no. 79" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import random\n", "print \"Enter minimum and maximum: \"\n", "m = int(raw_input())\n", "n = int(raw_input())\n", "# lowest and highest numbers\n", "r = n - m + 1\n", "# number of numbers in range\n", "for i in range(0,20):\n", " j = int(random.random()*100 % r + m)\n", " print j,\n", " print \" \",\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "Enter minimum and maximum: \n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "5\n" ] }, { "name": "stdout", "output_type": "stream", "stream": "stdout", "text": [ "15\n" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "6 15 10 8 15 9 7 7 11 6 5 15 14 15 15 15 11 13 14 6 \n" ] } ], "prompt_number": 24 } ], "metadata": {} } ] }