summaryrefslogtreecommitdiff
path: root/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2.ipynb
blob: 645ce13fa1cd7c73675b83f82067047b64040052 (plain)
1
{"nbformat_minor": 0, "cells": [{"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 01: Page 156", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "#To generate a sequence a_n=1/n\ni=1.0 #floating point division\nn=input(\"enter the number of terms in the sequence\");\nprint \"a_n=1/n\"\nprint \"when n=\",n,\"a_n is\"\nfor i in range(1,n+1): #iteration till the number of terms specified by the user\n    a=1.0/i\n    print \"1/\",i,\",\",\nprint \"\\n\"\nfor i in range(1,n+1): #iteration till the number of terms specified by the user\n    a=1.0/i\n    print a,\",\",\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter the number of terms in the sequence5\na_n=1/n\nwhen n= 5 a_n is\n1/ 1 , 1/ 2 , 1/ 3 , 1/ 4 , 1/ 5 , \n\n1.0 , 0.5 , 0.333333333333 , 0.25 , 0.2 ,\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 02: Page 157", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "n=input(\"Enter the number of terms in the sequence to generate the geometric progression\");\ni=1\nprint\"the list of terms\",\nfor i in range (n+1):print\"b\",i,\",\",\nprint \"begins with\", \nfor i in range (n+1): #iterate for the number of terms given as input\n    b_n=(-1)**i\n    print b_n,\nprint\"\\n\",\"the list of terms\",\nfor i in range (n+1):print\"c\",i,\",\",\nprint \"begins with\",   \nfor i in range (n+1): #iterate for the number of terms given as input\n    c_n=2*(5**i)\n    print c_n,\nprint\"\\n\",\"the list of terms\",\nfor i in range (n+1):print\"c\",i,\",\",\nprint \"begins with\",\nfor i in range (n+1): #iterate for the number of terms given as input\n    d_n=6.0*((1.0/3.0)**i)\n    print d_n, #prints the fraction values in decimals.  Floating point division\n\n    \n   \n    \n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of terms in the sequence to generate the geometric progression5\nthe list of terms b 0 , b 1 , b 2 , b 3 , b 4 , b 5 , begins with 1 -1 1 -1 1 -1 \nthe list of terms c 0 , c 1 , c 2 , c 3 , c 4 , c 5 , begins with 2 10 50 250 1250 6250 \nthe list of terms c 0 , c 1 , c 2 , c 3 , c 4 , c 5 , begins with 6.0 2.0 0.666666666667 0.222222222222 0.0740740740741 0.0246913580247\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 157", "cell_type": "markdown", "metadata": {}}, {"execution_count": 6, "cell_type": "code", "source": "n=input(\"Enter the number terms in the sequence\");\ns_n=-1+4*n\nt_n=7-3*n\ni=0\nprint \"The list of terms\",\nfor i in range(n):  \n    print \"s\",i,\",\",\nprint \"begins with\",\nfor i in range(n):#generates the sequence for -1*4i\n    print -1+4*i,\nprint \"\\nThe list of terms\",\nfor i in range(n):\n    print \"t\",i,\",\",\nprint \"begins with\",\nfor i in range(n):#generates the sequence for 7-3i\n    print 7-3*i,\n    \n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number terms in the sequence5\nThe list of terms s 0 , s 1 , s 2 , s 3 , s 4 , begins with -1 3 7 11 15 \nThe list of terms t 0 , t 1 , t 2 , t 3 , t 4 , begins with 7 4 1 -2 -5\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 05: Page 158", "cell_type": "markdown", "metadata": {}}, {"execution_count": 9, "cell_type": "code", "source": "a=[2,0,0,0] #assigning a[0]=2 (Given)\n\nfor i in range(1,4):#iteration to run till a[3]\n    a[i]=a[i-1]+3\n    print \"a[\",i,\"]\",a[i]", "outputs": [{"output_type": "stream", "name": "stdout", "text": "a[ 1 ] 5\na[ 2 ] 8\na[ 3 ] 11\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 06: Page 158", "cell_type": "markdown", "metadata": {}}, {"execution_count": 11, "cell_type": "code", "source": "a=[3,5,0,0] #assingning a[0],a[1] to the given values\n\nfor i in range(2,4): # iterations to find the successive values. If values are to be found for further terms the for loop \"stop\" has to be modified\n    a[i]=a[i-1]-a[i-2]\n    print \"a[\",i,\"]\",a[i]", "outputs": [{"output_type": "stream", "name": "stdout", "text": "a[ 2 ] 2\na[ 3 ] -3\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 07: Page 158 ", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "f=[0,1,0,0,0,0,0] #assingning a[0],a[1] to the given values\nprint \"Fibonacci series is\"\nfor i in range(2,7): # iterations to find the successive values. If values are to be found for further terms the for loop \"stop\" has to be modified\n    f[i]=f[i-1]+f[i-2]\n    print \"f[\",i,\"]=f[\",i-1,\"]+f[\",i-2,\"]=\",f[i]", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Fibonacci series is\nf[ 2 ]=f[ 1 ]+f[ 0 ]= 1\nf[ 3 ]=f[ 2 ]+f[ 1 ]= 2\nf[ 4 ]=f[ 3 ]+f[ 2 ]= 3\nf[ 5 ]=f[ 4 ]+f[ 3 ]= 5\nf[ 6 ]=f[ 5 ]+f[ 4 ]= 8\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 08: Page 159 ", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "n=1\nresult=0\nnumber=input(\"Enter the number\");\nfor i in range(1,number):\n    n=n+i*n \nprint \"The factorial of\",number,\"is\",n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number5\nThe factorial of 5 is 120\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 18: Page 164", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "#finding the summation of j^2\nup=input(\"Enter the upper limit for the operation j^2\");\nlow=input(\"Enter the lower limit for the operation j^2\");\nsum=0\nprint \"The square of terms form 1 to n\",\nfor j in range (low,up+1): #summation.  Iteration from lower to upper limit.\n    print j,\"^2+\",\n    j=j**2 #square function is computed as '**'\n    sum=sum+j\nprint \"=\",sum\n                \n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the upper limit for the operation j^25\nEnter the lower limit for the operation j^21\nThe square of terms form 1 to n 1 ^2+ 2 ^2+ 3 ^2+ 4 ^2+ 5 ^2+ = 55\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 19: Page 164", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "k=4 #lower limit\nsum=0\nprint \"The value for the sequence\",\nfor k in range (4,8+1,1): #8+1 , 8 is the upper limit, in python to make for loop run till the limit equal to upper limit we give a +1.\n    print \"(-1)^\",k,\"+\",\n    sum=sum+((-1)**k)\nprint \"=\",sum\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The value for the sequence (-1)^ 4 + (-1)^ 5 + (-1)^ 6 + (-1)^ 7 + (-1)^ 8 + = 1\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 21: Page 165", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "\nglobals()['j']=0\ni=0\nglobals()['s']=0\nupj=input(\"Enter the upper limit for the inner summation\");\nlowj=input(\"Enter the lower limit for the inner summation\");\nupi=input(\"Enter the upper limit for the outer summation\");\nlowi=input(\"Enter the lower limit for the outer summation\");\nfor i in range (lowj,upj+1):\n    j=j+i\nfor l in range(lowi,upi+1):\n    s=s+(j*l)\nprint s\n    \n    \n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the upper limit for the inner summation3\nEnter the lower limit for the inner summation1\nEnter the upper limit for the outer summation4\nEnter the lower limit for the outer summation1\n60\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {"collapsed": true}}, {"source": "## Example 13: Page 161", "cell_type": "markdown", "metadata": {"collapsed": true}}, {"execution_count": 4, "cell_type": "code", "source": "#To print series 1 once, 2 twice, 3 thrice and so on\na=[]\ni=1\nfor i in range(1,10+1): #for loop to initialise the number\n    for j in range(1,i+1):#for loop to iterate to make the count\n        print i,\n    ", "outputs": [{"output_type": "stream", "name": "stdout", "text": "1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 22: Page 166", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "s=0 #initialise it to zero to store the results\nglobals()['res']=0 #difining result as global variable since it has to be accessed outside the loop\nprint \"Sum of values of s for all the members of the set {\",\nfor s in range (0,4+1,2): #iterate for terms 0,4,6\n    print s,\n    res=res+s\nprint \"} is\",res\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Sum of values of s for all the members of the set { 0 2 4 } is 6\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 02: Page 178", "cell_type": "markdown", "metadata": {}}, {"execution_count": 6, "cell_type": "code", "source": "def getmat(): #function to get the matrix elements\n    m = int(input('number of rows, m = '))\n    n = int(input('number of columns, n = '))\n    matrix = []; columns = []\n\n    for i in range(0,m):\n        matrix.append([])\n        for j in range(0,n):\n            matrix[i].append(0)\n            print ('entry in row: ',i+1,' column: ',j+1)\n            matrix[i][j] = int(input())\n    return (matrix)\n\ndef matrixADD(m1,m2): #function to add the matrix.\n    z=[]\n    for i in range (len(m1)):\n        tem = []\n        for j in range (len(m2)):\n            x=m1[i][j]+m2[i][j]\n            tem.append(x)\n        z.append(tem)\n    return z    \n        \n\n\nmat1=[]\nmat2=[]\nZ=[]\nprint \"Enter the elements of matrix 1\"\nmat1=getmat() #function call\nprint \"Enter the elements of matrix 2\"\nmat2=getmat() #function call\nprint \"Addition of \\n matrix 1\",mat1,\"and \\n matrix 2\",mat2,\"is \\n\",matrixADD(mat1,mat2) #function call to add \n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the elements of matrix 1\nnumber of rows, m = 3\nnumber of columns, n = 3\n('entry in row: ', 1, ' column: ', 1)\n1\n('entry in row: ', 1, ' column: ', 2)\n0\n('entry in row: ', 1, ' column: ', 3)\n-1\n('entry in row: ', 2, ' column: ', 1)\n2\n('entry in row: ', 2, ' column: ', 2)\n2\n('entry in row: ', 2, ' column: ', 3)\n-3\n('entry in row: ', 3, ' column: ', 1)\n3\n('entry in row: ', 3, ' column: ', 2)\n4\n('entry in row: ', 3, ' column: ', 3)\n0\nEnter the elements of matrix 2\nnumber of rows, m = 3\nnumber of columns, n = 3\n('entry in row: ', 1, ' column: ', 1)\n3\n('entry in row: ', 1, ' column: ', 2)\n4\n('entry in row: ', 1, ' column: ', 3)\n-1\n('entry in row: ', 2, ' column: ', 1)\n1\n('entry in row: ', 2, ' column: ', 2)\n-3\n('entry in row: ', 2, ' column: ', 3)\n0\n('entry in row: ', 3, ' column: ', 1)\n-1\n('entry in row: ', 3, ' column: ', 2)\n1\n('entry in row: ', 3, ' column: ', 3)\n2\nAddition of \n matrix 1 [[1, 0, -1], [2, 2, -3], [3, 4, 0]] and \n matrix 2 [[3, 4, -1], [1, -3, 0], [-1, 1, 2]] is \n[[4, 4, -2], [3, -1, -3], [2, 5, 2]]\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 179", "cell_type": "markdown", "metadata": {}}, {"execution_count": 7, "cell_type": "code", "source": "\n# Program to multiply two matrices using nested loops\n\n# 3x3 matrix\nX = [[1,0,4],\n    [2,1,1],\n    [3,1,0],\n     [0,2,2]]\n# 3x4 matrix\nY = [[2,4],\n    [1,1],\n    [3,0]]\n# result is 3x4\nresult = [[0,0],\n         [0,0],\n         [0,0,],\n          [0,0]]\n\n# iterate through rows of X\nfor i in range(len(X)):\n   # iterate through columns of Y\n   for j in range(len(Y[0])):\n       # iterate through rows of Y\n       for k in range(len(Y)):\n           result[i][j] += X[i][k] * Y[k][j]\nprint \"The multiplication of the two matrices AB is\"\n\nfor r in result:\n   print(r)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The multiplication of the two matrices AB is\n[14, 4]\n[8, 9]\n[7, 13]\n[8, 2]\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 02: Basic Structures: Sets, Functions, Sequences, Sums and Matrices", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 05: Page 181", "cell_type": "markdown", "metadata": {}}, {"execution_count": 8, "cell_type": "code", "source": "# Program to transpose a matrix using nested loop\n# iterate through rows\ndef mattrans(X,result):\n    print \"The transpose is\"\n    for i in range(len(X)):\n   # iterate through columns\n       for j in range(len(X[0])):\n           result[j][i] = X[i][j]\n    for r in result:\n        print(r)\n        \ndef getmat():\n    row = int(input('number of rows = '))\n    col = int(input('number of columns = '))\n    matrix = []; columns = []\n\n    for i in range(0,row):\n        matrix.append([])\n        for j in range(0,col):\n            matrix[i].append(0)\n            print ('entry in row: ',i+1,' column: ',j+1)\n            matrix[i][j] = int(input())\n    for c in range(col):\n        for r in range(row):\n            result[c][r]=0\n    mattrans(matrix,result)\n    \n\n\nprint \"Enter the elements of the matrix\"\ngetmat()\n#mattrans(mat1)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the elements of the matrix\nnumber of rows = 2\nnumber of columns = 3\n('entry in row: ', 1, ' column: ', 1)\n1\n('entry in row: ', 1, ' column: ', 2)\n2\n('entry in row: ', 1, ' column: ', 3)\n3\n('entry in row: ', 2, ' column: ', 1)\n4\n('entry in row: ', 2, ' column: ', 2)\n5\n('entry in row: ', 2, ' column: ', 3)\n6\nThe transpose is\n[1, 4]\n[2, 5]\n[3, 6]\n[8, 2]\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "", "cell_type": "markdown", "metadata": {}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.9", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}}