summaryrefslogtreecommitdiff
path: root/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen
diff options
context:
space:
mode:
Diffstat (limited to 'Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen')
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2.ipynb1
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_(3).ipynb818
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_(3)_1.ipynb818
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_1.ipynb1
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1.ipynb1
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_(3).ipynb394
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_(3)_1.ipynb394
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_1.ipynb1
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3.ipynb144
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_1.ipynb144
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_2.ipynb1
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_3.ipynb1
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4.ipynb743
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_1.ipynb743
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_2.ipynb1
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_3.ipynb1
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5.ipynb312
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_1.ipynb312
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_2.ipynb1
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_3.ipynb1
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6.ipynb1
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_(1).ipynb594
-rwxr-xr-xDiscrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_(1)_1.ipynb594
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_1.ipynb1
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch2.pngbin0 -> 39938 bytes
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch2_1.pngbin0 -> 39938 bytes
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch4.pngbin0 -> 52842 bytes
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch4_1.pngbin0 -> 52842 bytes
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch5.pngbin0 -> 39165 bytes
-rw-r--r--Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/chapter5.ipynb312
30 files changed, 6334 insertions, 0 deletions
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2.ipynb
new file mode 100644
index 00000000..645ce13f
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2.ipynb
@@ -0,0 +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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_(3).ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_(3).ipynb
new file mode 100755
index 00000000..494652e8
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_(3).ipynb
@@ -0,0 +1,818 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 01: Page 156"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "enter the number of terms in the sequence5\n",
+ "a_n=1/n\n",
+ "when n= 5 a_n is\n",
+ "1/ 1 , 1/ 2 , 1/ 3 , 1/ 4 , 1/ 5 , \n",
+ "\n",
+ "1.0 , 0.5 , 0.333333333333 , 0.25 , 0.2 ,\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To generate a sequence a_n=1/n\n",
+ "i=1.0 #floating point division\n",
+ "n=input(\"enter the number of terms in the sequence\");\n",
+ "print \"a_n=1/n\"\n",
+ "print \"when n=\",n,\"a_n is\"\n",
+ "for 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,\",\",\n",
+ "print \"\\n\"\n",
+ "for 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"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 02: Page 157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of terms in the sequence to generate the geometric progression5\n",
+ "the list of terms b 0 , b 1 , b 2 , b 3 , b 4 , b 5 , begins with 1 -1 1 -1 1 -1 \n",
+ "the list of terms c 0 , c 1 , c 2 , c 3 , c 4 , c 5 , begins with 2 10 50 250 1250 6250 \n",
+ "the 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"
+ ]
+ }
+ ],
+ "source": [
+ "n=input(\"Enter the number of terms in the sequence to generate the geometric progression\");\n",
+ "i=1\n",
+ "print\"the list of terms\",\n",
+ "for i in range (n+1):print\"b\",i,\",\",\n",
+ "print \"begins with\", \n",
+ "for i in range (n+1): #iterate for the number of terms given as input\n",
+ " b_n=(-1)**i\n",
+ " print b_n,\n",
+ "print\"\\n\",\"the list of terms\",\n",
+ "for i in range (n+1):print\"c\",i,\",\",\n",
+ "print \"begins with\", \n",
+ "for i in range (n+1): #iterate for the number of terms given as input\n",
+ " c_n=2*(5**i)\n",
+ " print c_n,\n",
+ "print\"\\n\",\"the list of terms\",\n",
+ "for i in range (n+1):print\"c\",i,\",\",\n",
+ "print \"begins with\",\n",
+ "for 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"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number terms in the sequence5\n",
+ "The list of terms s 0 , s 1 , s 2 , s 3 , s 4 , begins with -1 3 7 11 15 \n",
+ "The list of terms t 0 , t 1 , t 2 , t 3 , t 4 , begins with 7 4 1 -2 -5\n"
+ ]
+ }
+ ],
+ "source": [
+ "n=input(\"Enter the number terms in the sequence\");\n",
+ "s_n=-1+4*n\n",
+ "t_n=7-3*n\n",
+ "i=0\n",
+ "print \"The list of terms\",\n",
+ "for i in range(n):\n",
+ " print \"s\",i,\",\",\n",
+ "print \"begins with\",\n",
+ "for i in range(n):\n",
+ " print -1+4*i,\n",
+ "print \"\\nThe list of terms\",\n",
+ "for i in range(n):\n",
+ " print \"t\",i,\",\",\n",
+ "print \"begins with\",\n",
+ "for i in range(n):\n",
+ " print 7-3*i,\n",
+ " \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 05: Page 158"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "a[ 1 ] 5\n",
+ "a[ 2 ] 8\n",
+ "a[ 3 ] 11\n"
+ ]
+ }
+ ],
+ "source": [
+ "a=[2,0,0,0] #assigning a[0]=2 (Given)\n",
+ "\n",
+ "for i in range(1,4):#iteration to run till a[3]\n",
+ " a[i]=a[i-1]+3\n",
+ " print \"a[\",i,\"]\",a[i]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 06: Page 158"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "a[ 2 ] 2\n",
+ "a[ 3 ] -3\n"
+ ]
+ }
+ ],
+ "source": [
+ "a=[3,5,0,0] #assingning a[0],a[1] to the given values\n",
+ "\n",
+ "for 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]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 07: Page 158 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Fibonacci series is\n",
+ "f[ 2 ]=f[ 1 ]+f[ 0 ]= 1\n",
+ "f[ 3 ]=f[ 2 ]+f[ 1 ]= 2\n",
+ "f[ 4 ]=f[ 3 ]+f[ 2 ]= 3\n",
+ "f[ 5 ]=f[ 4 ]+f[ 3 ]= 5\n",
+ "f[ 6 ]=f[ 5 ]+f[ 4 ]= 8\n"
+ ]
+ }
+ ],
+ "source": [
+ "f=[0,1,0,0,0,0,0] #assingning a[0],a[1] to the given values\n",
+ "print \"Fibonacci series is\"\n",
+ "for 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]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 08: Page 159 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number5\n",
+ "The factorial of 5 is 120\n"
+ ]
+ }
+ ],
+ "source": [
+ "n=1\n",
+ "result=0\n",
+ "number=input(\"Enter the number\");\n",
+ "for i in range(1,number):\n",
+ " n=n+i*n \n",
+ "print \"The factorial of\",number,\"is\",n\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 18: Page 164"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the upper limit for the operation j^25\n",
+ "Enter the lower limit for the operation j^21\n",
+ "The square of terms form 1 to n 1 ^2+ 2 ^2+ 3 ^2+ 4 ^2+ 5 ^2+ = 55\n"
+ ]
+ }
+ ],
+ "source": [
+ "#finding the summation of j^2\n",
+ "up=input(\"Enter the upper limit for the operation j^2\");\n",
+ "low=input(\"Enter the lower limit for the operation j^2\");\n",
+ "sum=0\n",
+ "print \"The square of terms form 1 to n\",\n",
+ "for 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\n",
+ "print \"=\",sum\n",
+ " \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 19: Page 164"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The value for the sequence (-1)^ 4 + (-1)^ 5 + (-1)^ 6 + (-1)^ 7 + (-1)^ 8 + = 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "k=4 #lower limit\n",
+ "sum=0\n",
+ "print \"The value for the sequence\",\n",
+ "for 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)\n",
+ "print \"=\",sum\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 21: Page 165"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the upper limit for the inner summation3\n",
+ "Enter the lower limit for the inner summation1\n",
+ "Enter the upper limit for the outer summation4\n",
+ "Enter the lower limit for the outer summation1\n",
+ "60\n"
+ ]
+ }
+ ],
+ "source": [
+ "\n",
+ "globals()['j']=0\n",
+ "i=0\n",
+ "globals()['s']=0\n",
+ "upj=input(\"Enter the upper limit for the inner summation\");\n",
+ "lowj=input(\"Enter the lower limit for the inner summation\");\n",
+ "upi=input(\"Enter the upper limit for the outer summation\");\n",
+ "lowi=input(\"Enter the lower limit for the outer summation\");\n",
+ "for i in range (lowj,upj+1):\n",
+ " j=j+i\n",
+ "for l in range(lowi,upi+1):\n",
+ " s=s+(j*l)\n",
+ "print s\n",
+ " \n",
+ " \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "## Example 13: Page 161"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "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"
+ ]
+ }
+ ],
+ "source": [
+ "#To print series 1 once, 2 twice, 3 thrice and so on\n",
+ "a=[]\n",
+ "i=1\n",
+ "for 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",
+ " "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 22: Page 166"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Sum of values of s for all the members of the set { 0 2 4 } is 6\n"
+ ]
+ }
+ ],
+ "source": [
+ "s=0 #initialise it to zero to store the results\n",
+ "globals()['res']=0 #difining result as global variable since it has to be accessed outside the loop\n",
+ "print \"Sum of values of s for all the members of the set {\",\n",
+ "for s in range (0,4+1,2): #iterate for terms 0,4,6\n",
+ " print s,\n",
+ " res=res+s\n",
+ "print \"} is\",res\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 02: Page 178"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the elements of matrix 1\n",
+ "number of rows, m = 3\n",
+ "number of columns, n = 3\n",
+ "('entry in row: ', 1, ' column: ', 1)\n",
+ "1\n",
+ "('entry in row: ', 1, ' column: ', 2)\n",
+ "0\n",
+ "('entry in row: ', 1, ' column: ', 3)\n",
+ "-1\n",
+ "('entry in row: ', 2, ' column: ', 1)\n",
+ "2\n",
+ "('entry in row: ', 2, ' column: ', 2)\n",
+ "2\n",
+ "('entry in row: ', 2, ' column: ', 3)\n",
+ "-3\n",
+ "('entry in row: ', 3, ' column: ', 1)\n",
+ "3\n",
+ "('entry in row: ', 3, ' column: ', 2)\n",
+ "4\n",
+ "('entry in row: ', 3, ' column: ', 3)\n",
+ "0\n",
+ "Enter the elements of matrix 2\n",
+ "number of rows, m = 3\n",
+ "number of columns, n = 3\n",
+ "('entry in row: ', 1, ' column: ', 1)\n",
+ "3\n",
+ "('entry in row: ', 1, ' column: ', 2)\n",
+ "4\n",
+ "('entry in row: ', 1, ' column: ', 3)\n",
+ "-1\n",
+ "('entry in row: ', 2, ' column: ', 1)\n",
+ "1\n",
+ "('entry in row: ', 2, ' column: ', 2)\n",
+ "-3\n",
+ "('entry in row: ', 2, ' column: ', 3)\n",
+ "0\n",
+ "('entry in row: ', 3, ' column: ', 1)\n",
+ "-1\n",
+ "('entry in row: ', 3, ' column: ', 2)\n",
+ "1\n",
+ "('entry in row: ', 3, ' column: ', 3)\n",
+ "2\n",
+ "Addition 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"
+ ]
+ }
+ ],
+ "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",
+ "\n",
+ "def 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",
+ "\n",
+ "mat1=[]\n",
+ "mat2=[]\n",
+ "Z=[]\n",
+ "print \"Enter the elements of matrix 1\"\n",
+ "mat1=getmat() #function call\n",
+ "print \"Enter the elements of matrix 2\"\n",
+ "mat2=getmat() #function call\n",
+ "print \"Addition of \\n matrix 1\",mat1,\"and \\n matrix 2\",mat2,\"is \\n\",matrixADD(mat1,mat2) #function call to add \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 179"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The multiplication of the two matrices AB is\n",
+ "[14, 4]\n",
+ "[8, 9]\n",
+ "[7, 13]\n",
+ "[8, 2]\n"
+ ]
+ }
+ ],
+ "source": [
+ "\n",
+ "# Program to multiply two matrices using nested loops\n",
+ "\n",
+ "# 3x3 matrix\n",
+ "X = [[1,0,4],\n",
+ " [2,1,1],\n",
+ " [3,1,0],\n",
+ " [0,2,2]]\n",
+ "# 3x4 matrix\n",
+ "Y = [[2,4],\n",
+ " [1,1],\n",
+ " [3,0]]\n",
+ "# result is 3x4\n",
+ "result = [[0,0],\n",
+ " [0,0],\n",
+ " [0,0,],\n",
+ " [0,0]]\n",
+ "\n",
+ "# iterate through rows of X\n",
+ "for 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]\n",
+ "print \"The multiplication of the two matrices AB is\"\n",
+ "\n",
+ "for r in result:\n",
+ " print(r)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 05: Page 181"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the elements of the matrix\n",
+ "number of rows = 2\n",
+ "number of columns = 3\n",
+ "('entry in row: ', 1, ' column: ', 1)\n",
+ "1\n",
+ "('entry in row: ', 1, ' column: ', 2)\n",
+ "2\n",
+ "('entry in row: ', 1, ' column: ', 3)\n",
+ "3\n",
+ "('entry in row: ', 2, ' column: ', 1)\n",
+ "4\n",
+ "('entry in row: ', 2, ' column: ', 2)\n",
+ "5\n",
+ "('entry in row: ', 2, ' column: ', 3)\n",
+ "6\n",
+ "The transpose is\n",
+ "[1, 4]\n",
+ "[2, 5]\n",
+ "[3, 6]\n",
+ "[8, 2]\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Program to transpose a matrix using nested loop\n",
+ "# iterate through rows\n",
+ "def 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",
+ " \n",
+ "def 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",
+ "\n",
+ "print \"Enter the elements of the matrix\"\n",
+ "getmat()\n",
+ "#mattrans(mat1)\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_(3)_1.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_(3)_1.ipynb
new file mode 100755
index 00000000..494652e8
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_(3)_1.ipynb
@@ -0,0 +1,818 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 01: Page 156"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "enter the number of terms in the sequence5\n",
+ "a_n=1/n\n",
+ "when n= 5 a_n is\n",
+ "1/ 1 , 1/ 2 , 1/ 3 , 1/ 4 , 1/ 5 , \n",
+ "\n",
+ "1.0 , 0.5 , 0.333333333333 , 0.25 , 0.2 ,\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To generate a sequence a_n=1/n\n",
+ "i=1.0 #floating point division\n",
+ "n=input(\"enter the number of terms in the sequence\");\n",
+ "print \"a_n=1/n\"\n",
+ "print \"when n=\",n,\"a_n is\"\n",
+ "for 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,\",\",\n",
+ "print \"\\n\"\n",
+ "for 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"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 02: Page 157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of terms in the sequence to generate the geometric progression5\n",
+ "the list of terms b 0 , b 1 , b 2 , b 3 , b 4 , b 5 , begins with 1 -1 1 -1 1 -1 \n",
+ "the list of terms c 0 , c 1 , c 2 , c 3 , c 4 , c 5 , begins with 2 10 50 250 1250 6250 \n",
+ "the 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"
+ ]
+ }
+ ],
+ "source": [
+ "n=input(\"Enter the number of terms in the sequence to generate the geometric progression\");\n",
+ "i=1\n",
+ "print\"the list of terms\",\n",
+ "for i in range (n+1):print\"b\",i,\",\",\n",
+ "print \"begins with\", \n",
+ "for i in range (n+1): #iterate for the number of terms given as input\n",
+ " b_n=(-1)**i\n",
+ " print b_n,\n",
+ "print\"\\n\",\"the list of terms\",\n",
+ "for i in range (n+1):print\"c\",i,\",\",\n",
+ "print \"begins with\", \n",
+ "for i in range (n+1): #iterate for the number of terms given as input\n",
+ " c_n=2*(5**i)\n",
+ " print c_n,\n",
+ "print\"\\n\",\"the list of terms\",\n",
+ "for i in range (n+1):print\"c\",i,\",\",\n",
+ "print \"begins with\",\n",
+ "for 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"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 157"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number terms in the sequence5\n",
+ "The list of terms s 0 , s 1 , s 2 , s 3 , s 4 , begins with -1 3 7 11 15 \n",
+ "The list of terms t 0 , t 1 , t 2 , t 3 , t 4 , begins with 7 4 1 -2 -5\n"
+ ]
+ }
+ ],
+ "source": [
+ "n=input(\"Enter the number terms in the sequence\");\n",
+ "s_n=-1+4*n\n",
+ "t_n=7-3*n\n",
+ "i=0\n",
+ "print \"The list of terms\",\n",
+ "for i in range(n):\n",
+ " print \"s\",i,\",\",\n",
+ "print \"begins with\",\n",
+ "for i in range(n):\n",
+ " print -1+4*i,\n",
+ "print \"\\nThe list of terms\",\n",
+ "for i in range(n):\n",
+ " print \"t\",i,\",\",\n",
+ "print \"begins with\",\n",
+ "for i in range(n):\n",
+ " print 7-3*i,\n",
+ " \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 05: Page 158"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "a[ 1 ] 5\n",
+ "a[ 2 ] 8\n",
+ "a[ 3 ] 11\n"
+ ]
+ }
+ ],
+ "source": [
+ "a=[2,0,0,0] #assigning a[0]=2 (Given)\n",
+ "\n",
+ "for i in range(1,4):#iteration to run till a[3]\n",
+ " a[i]=a[i-1]+3\n",
+ " print \"a[\",i,\"]\",a[i]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 06: Page 158"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "a[ 2 ] 2\n",
+ "a[ 3 ] -3\n"
+ ]
+ }
+ ],
+ "source": [
+ "a=[3,5,0,0] #assingning a[0],a[1] to the given values\n",
+ "\n",
+ "for 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]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 07: Page 158 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Fibonacci series is\n",
+ "f[ 2 ]=f[ 1 ]+f[ 0 ]= 1\n",
+ "f[ 3 ]=f[ 2 ]+f[ 1 ]= 2\n",
+ "f[ 4 ]=f[ 3 ]+f[ 2 ]= 3\n",
+ "f[ 5 ]=f[ 4 ]+f[ 3 ]= 5\n",
+ "f[ 6 ]=f[ 5 ]+f[ 4 ]= 8\n"
+ ]
+ }
+ ],
+ "source": [
+ "f=[0,1,0,0,0,0,0] #assingning a[0],a[1] to the given values\n",
+ "print \"Fibonacci series is\"\n",
+ "for 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]"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 08: Page 159 "
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number5\n",
+ "The factorial of 5 is 120\n"
+ ]
+ }
+ ],
+ "source": [
+ "n=1\n",
+ "result=0\n",
+ "number=input(\"Enter the number\");\n",
+ "for i in range(1,number):\n",
+ " n=n+i*n \n",
+ "print \"The factorial of\",number,\"is\",n\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 18: Page 164"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the upper limit for the operation j^25\n",
+ "Enter the lower limit for the operation j^21\n",
+ "The square of terms form 1 to n 1 ^2+ 2 ^2+ 3 ^2+ 4 ^2+ 5 ^2+ = 55\n"
+ ]
+ }
+ ],
+ "source": [
+ "#finding the summation of j^2\n",
+ "up=input(\"Enter the upper limit for the operation j^2\");\n",
+ "low=input(\"Enter the lower limit for the operation j^2\");\n",
+ "sum=0\n",
+ "print \"The square of terms form 1 to n\",\n",
+ "for 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\n",
+ "print \"=\",sum\n",
+ " \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 19: Page 164"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The value for the sequence (-1)^ 4 + (-1)^ 5 + (-1)^ 6 + (-1)^ 7 + (-1)^ 8 + = 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "k=4 #lower limit\n",
+ "sum=0\n",
+ "print \"The value for the sequence\",\n",
+ "for 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)\n",
+ "print \"=\",sum\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 21: Page 165"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the upper limit for the inner summation3\n",
+ "Enter the lower limit for the inner summation1\n",
+ "Enter the upper limit for the outer summation4\n",
+ "Enter the lower limit for the outer summation1\n",
+ "60\n"
+ ]
+ }
+ ],
+ "source": [
+ "\n",
+ "globals()['j']=0\n",
+ "i=0\n",
+ "globals()['s']=0\n",
+ "upj=input(\"Enter the upper limit for the inner summation\");\n",
+ "lowj=input(\"Enter the lower limit for the inner summation\");\n",
+ "upi=input(\"Enter the upper limit for the outer summation\");\n",
+ "lowi=input(\"Enter the lower limit for the outer summation\");\n",
+ "for i in range (lowj,upj+1):\n",
+ " j=j+i\n",
+ "for l in range(lowi,upi+1):\n",
+ " s=s+(j*l)\n",
+ "print s\n",
+ " \n",
+ " \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "## Example 13: Page 161"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "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"
+ ]
+ }
+ ],
+ "source": [
+ "#To print series 1 once, 2 twice, 3 thrice and so on\n",
+ "a=[]\n",
+ "i=1\n",
+ "for 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",
+ " "
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02: BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 22: Page 166"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Sum of values of s for all the members of the set { 0 2 4 } is 6\n"
+ ]
+ }
+ ],
+ "source": [
+ "s=0 #initialise it to zero to store the results\n",
+ "globals()['res']=0 #difining result as global variable since it has to be accessed outside the loop\n",
+ "print \"Sum of values of s for all the members of the set {\",\n",
+ "for s in range (0,4+1,2): #iterate for terms 0,4,6\n",
+ " print s,\n",
+ " res=res+s\n",
+ "print \"} is\",res\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 02: Page 178"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the elements of matrix 1\n",
+ "number of rows, m = 3\n",
+ "number of columns, n = 3\n",
+ "('entry in row: ', 1, ' column: ', 1)\n",
+ "1\n",
+ "('entry in row: ', 1, ' column: ', 2)\n",
+ "0\n",
+ "('entry in row: ', 1, ' column: ', 3)\n",
+ "-1\n",
+ "('entry in row: ', 2, ' column: ', 1)\n",
+ "2\n",
+ "('entry in row: ', 2, ' column: ', 2)\n",
+ "2\n",
+ "('entry in row: ', 2, ' column: ', 3)\n",
+ "-3\n",
+ "('entry in row: ', 3, ' column: ', 1)\n",
+ "3\n",
+ "('entry in row: ', 3, ' column: ', 2)\n",
+ "4\n",
+ "('entry in row: ', 3, ' column: ', 3)\n",
+ "0\n",
+ "Enter the elements of matrix 2\n",
+ "number of rows, m = 3\n",
+ "number of columns, n = 3\n",
+ "('entry in row: ', 1, ' column: ', 1)\n",
+ "3\n",
+ "('entry in row: ', 1, ' column: ', 2)\n",
+ "4\n",
+ "('entry in row: ', 1, ' column: ', 3)\n",
+ "-1\n",
+ "('entry in row: ', 2, ' column: ', 1)\n",
+ "1\n",
+ "('entry in row: ', 2, ' column: ', 2)\n",
+ "-3\n",
+ "('entry in row: ', 2, ' column: ', 3)\n",
+ "0\n",
+ "('entry in row: ', 3, ' column: ', 1)\n",
+ "-1\n",
+ "('entry in row: ', 3, ' column: ', 2)\n",
+ "1\n",
+ "('entry in row: ', 3, ' column: ', 3)\n",
+ "2\n",
+ "Addition 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"
+ ]
+ }
+ ],
+ "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",
+ "\n",
+ "def 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",
+ "\n",
+ "mat1=[]\n",
+ "mat2=[]\n",
+ "Z=[]\n",
+ "print \"Enter the elements of matrix 1\"\n",
+ "mat1=getmat() #function call\n",
+ "print \"Enter the elements of matrix 2\"\n",
+ "mat2=getmat() #function call\n",
+ "print \"Addition of \\n matrix 1\",mat1,\"and \\n matrix 2\",mat2,\"is \\n\",matrixADD(mat1,mat2) #function call to add \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 179"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The multiplication of the two matrices AB is\n",
+ "[14, 4]\n",
+ "[8, 9]\n",
+ "[7, 13]\n",
+ "[8, 2]\n"
+ ]
+ }
+ ],
+ "source": [
+ "\n",
+ "# Program to multiply two matrices using nested loops\n",
+ "\n",
+ "# 3x3 matrix\n",
+ "X = [[1,0,4],\n",
+ " [2,1,1],\n",
+ " [3,1,0],\n",
+ " [0,2,2]]\n",
+ "# 3x4 matrix\n",
+ "Y = [[2,4],\n",
+ " [1,1],\n",
+ " [3,0]]\n",
+ "# result is 3x4\n",
+ "result = [[0,0],\n",
+ " [0,0],\n",
+ " [0,0,],\n",
+ " [0,0]]\n",
+ "\n",
+ "# iterate through rows of X\n",
+ "for 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]\n",
+ "print \"The multiplication of the two matrices AB is\"\n",
+ "\n",
+ "for r in result:\n",
+ " print(r)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 02:BASIC STRUCTURES: SETS, FUNCTIONS, SEQUENCES, SUMS AND MATRICES"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 05: Page 181"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the elements of the matrix\n",
+ "number of rows = 2\n",
+ "number of columns = 3\n",
+ "('entry in row: ', 1, ' column: ', 1)\n",
+ "1\n",
+ "('entry in row: ', 1, ' column: ', 2)\n",
+ "2\n",
+ "('entry in row: ', 1, ' column: ', 3)\n",
+ "3\n",
+ "('entry in row: ', 2, ' column: ', 1)\n",
+ "4\n",
+ "('entry in row: ', 2, ' column: ', 2)\n",
+ "5\n",
+ "('entry in row: ', 2, ' column: ', 3)\n",
+ "6\n",
+ "The transpose is\n",
+ "[1, 4]\n",
+ "[2, 5]\n",
+ "[3, 6]\n",
+ "[8, 2]\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Program to transpose a matrix using nested loop\n",
+ "# iterate through rows\n",
+ "def 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",
+ " \n",
+ "def 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",
+ "\n",
+ "print \"Enter the elements of the matrix\"\n",
+ "getmat()\n",
+ "#mattrans(mat1)\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_1.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_1.ipynb
new file mode 100644
index 00000000..645ce13f
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chap2_1.ipynb
@@ -0,0 +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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1.ipynb
new file mode 100644
index 00000000..fe652a17
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1.ipynb
@@ -0,0 +1 @@
+{"nbformat_minor": 0, "cells": [{"source": "#Chapter 01: The Foundations: Logic and Proofs\n", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 01:Page 02", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "print \"The following sentences are Propositions\" #Proposition should be a declarative sentence or should result in either a YES or a NO.\n\nprint \"1. Washington D.C is the capital of the United States of America\\n2. Toronto is the capital of Canada\\n3. 1+1=2.\\n4. 2+2=3.\" #Since these statements are declarative and they answer the question YES or NO they are called propositions.\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The following sentences are Propositions\n1. Washington D.C is the capital of the United States of America\n2. Toronto is the capital of Canada\n3. 1+1=2.\n4. 2+2=3.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 02:Page 02", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "print \"1. What time is it? \\n2. Read this carefully. \\n3. x+1=2.\\n4. x+y=Z.\"\nprint\"Sentences 1 and 2 are not propositions since they are not declarative. Sentences 3 and 4 are neither true nor false and so they are not propositions.\"\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "1. What time is it? \n2. Read this carefully. \n3. x+1=2.\n4. x+y=Z.\nSentences 1 and 2 are not propositions since they are not declarative. Sentences 3 and 4 are neither true nor false and so they are not propositions.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 03:Page 03", "cell_type": "markdown", "metadata": {}}, {"execution_count": 9, "cell_type": "code", "source": "print \"Propositon p=Michael's PC runs Linux.\"\nprint \"\\n Negation of p is ~p : It is not the case that Michael's PC runs Linux.\"\nprint \"\\n Negation of p is ~p : Michae's PC does not run.\"#Negation is opposite of the truth value of the proposition expressed with \"it is not the case that\" or with \"not\".\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Propositon p=Michael's PC runs Linux.\n\n Negation of p is ~p : It is not the case that Michael's PC runs Linux.\n\n Negation of p is ~p : Michae's PC does not run.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 04:Page 03", "cell_type": "markdown", "metadata": {}}, {"execution_count": 10, "cell_type": "code", "source": "print \"Let p=Vandana's smartphone has at least 32GB of memory.\"\nprint \"The negation of p is ( ~p ) :It is not the case that Vandana's smartphone has at least 32GB of memory.\"\nprint \"Or in simple English ( ~p ): Vandana's smartphone does not have at least 32GB of memory.\"\nprint \"Or even more simple as ( ~p ): Vandana's smartphone has less than 32GB of memory.\"\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Let p=Vandana's smartphone has at least 32GB of memory.\nThe negation of p is ( ~p ) :It is not the case that Vandana's smartphone has at least 32GB of memory.\nOr in simple English ( ~p ): Vandana's smartphone does not have at least 32GB of memory.\nOr even more simple as ( ~p ): Vandana's smartphone has less than 32GB of memory.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 05:Page 04", "cell_type": "markdown", "metadata": {}}, {"execution_count": 11, "cell_type": "code", "source": "p=\"Rebecca's PC has more than 16GB free hard disk space\"\nq=\"The processor in Rebecca's PC runs faster than 1GHz\"\nprint \"Let p,q be two propositions\"\nprint \"Let p=\",p,\"\\n\",\"Let q=\",q\nprint \"Conjunction of p^q is : \"+p+\" and \"+q #conjunction combines two propositons with \"and\"\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Let p,q be two propositions\nLet p= Rebecca's PC has more than 16GB free hard disk space \nLet q= The processor in Rebecca's PC runs faster than 1GHz\nConjunction of p^q is : Rebecca's PC has more than 16GB free hard disk space and The processor in Rebecca's PC runs faster than 1GHz\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 06:Page 05", "cell_type": "markdown", "metadata": {}}, {"execution_count": 12, "cell_type": "code", "source": "p=\"Rebecca's PC has more than 16GB free hard disk space\"\nq=\"The processor in Rebecca's PC runs faster than 1GHz\"\nprint \"Let p,q be two propositions\"\nprint \"Let p=\",p,\"\\n\",\"Let q=\",q\nprint \"Disjunction of p\\/q is : \"+p+\" or \"+q #unavailability of cup symbol. So \\/\n#Disjunction combines two propositons using OR \n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Let p,q be two propositions\nLet p= Rebecca's PC has more than 16GB free hard disk space \nLet q= The processor in Rebecca's PC runs faster than 1GHz\nDisjunction of p\\/q is : Rebecca's PC has more than 16GB free hard disk space or The processor in Rebecca's PC runs faster than 1GHz\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 07:Page 07", "cell_type": "markdown", "metadata": {}}, {"execution_count": 14, "cell_type": "code", "source": "p=\"Maria learns discrete mathematics\"\nq=\"Maria will find a good job\"\nprint\"Let p=\",p,\"\\n\",\"Let q=\",q\nprint\"p->q is : \"+\"If \"+p+\" then \"+q #p->q p implies q means If P then Q.\nprint\"p->q is also expressed as :\",q,\" when \",p\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Let p= Maria learns discrete mathematics \nLet q= Maria will find a good job\np->q is : If Maria learns discrete mathematics then Maria will find a good job\np->q is also expressed as : Maria will find a good job when Maria learns discrete mathematics\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {"collapsed": true}}, {"source": "## Example 01:Page 37", "cell_type": "markdown", "metadata": {}}, {"execution_count": 15, "cell_type": "code", "source": "def p(x): #Function defined to check whether the given statements are true.\n if(x>3):\n print \"p(\",x,\") which is the statement\",x,\">3, is true\"\n else:\n print \"p(\",x,\") which is the statement\",x,\">3, is false\"\np(4)#Fuction call \np(2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "p( 4 ) which is the statement 4 >3, is true\np( 2 ) which is the statement 2 >3, is false\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 02:Page 38", "cell_type": "markdown", "metadata": {}}, {"execution_count": 17, "cell_type": "code", "source": "x1=\"CS1\" #Defining systems to check whether they are under attack through a function.\nx2=\"CS2\"\nx3=\"MATH1\"\ndef A(x):\n if(x==\"CS1\"): #Since cs1 and Math1 are the two computers under attack\n print \"A(\",x,\") is true.\"\n else:\n if(x==\"MATH1\"): #Since CS1 and MATH1 are the two computers under attack\n print \"A(\",x,\") is true.\"\n else:\n print\"A(\",x,\") is false.\"\nprint \"Systems under attack are CS1 and MATH1. The truth values for the same are calculated using functions.\"\nA(x1)#Function call \nA(x2)\nA(x3)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Systems under attack are CS1 and MATH1. The truth values for the same are calculated using functions.\nA( CS1 ) is true.\nA( CS2 ) is false.\nA( MATH1 ) is true.\n"}], "metadata": {"collapsed": false, "trusted": true}}], "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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_(3).ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_(3).ipynb
new file mode 100755
index 00000000..408d2fa2
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_(3).ipynb
@@ -0,0 +1,394 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 01:Page 02"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The following sentences are Propositions\n",
+ "1. Washington D.C is the capital of the United States of America\n",
+ "2. Toronto is the capital of Canada\n",
+ "3. 1+1=2.\n",
+ "4. 2+2=3.\n"
+ ]
+ }
+ ],
+ "source": [
+ "print \"The following sentences are Propositions\" #Proposition should be a declarative sentence or should result in either a YES or a NO.\n",
+ "\n",
+ "print \"1. Washington D.C is the capital of the United States of America\\n2. Toronto is the capital of Canada\\n3. 1+1=2.\\n4. 2+2=3.\" #Since these statements are declarative and they answer the question YES or NO they are called propositions.\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 02:Page 02"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "1. What time is it? \n",
+ "2. Read this carefully. \n",
+ "3. x+1=2.\n",
+ "4. x+y=Z.\n",
+ "Sentences 1 and 2 are not propositions since they are not declarative. Sentences 3 and 4 are neither true nor false and so they are not propositions.\n"
+ ]
+ }
+ ],
+ "source": [
+ "print \"1. What time is it? \\n2. Read this carefully. \\n3. x+1=2.\\n4. x+y=Z.\"\n",
+ "print\"Sentences 1 and 2 are not propositions since they are not declarative. Sentences 3 and 4 are neither true nor false and so they are not propositions.\"\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 03:Page 03"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Propositon p=Michael's PC runs Linux.\n",
+ "\n",
+ " Negation of p is ~p : It is not the case that Michael's PC runs Linux.\n",
+ "\n",
+ " Negation of p is ~p : Michae's PC does not run.\n"
+ ]
+ }
+ ],
+ "source": [
+ "print \"Propositon p=Michael's PC runs Linux.\"\n",
+ "print \"\\n Negation of p is ~p : It is not the case that Michael's PC runs Linux.\"\n",
+ "print \"\\n Negation of p is ~p : Michae's PC does not run.\"#Negation is opposite of the truth value of the proposition expressed with \"it is not the case that\" or with \"not\".\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 04:Page 03"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Let p=Vandana's smartphone has at least 32GB of memory.\n",
+ "The negation of p is ( ~p ) :It is not the case that Vandana's smartphone has at least 32GB of memory.\n",
+ "Or in simple English ( ~p ): Vandana's smartphone does not have at least 32GB of memory.\n",
+ "Or even more simple as ( ~p ): Vandana's smartphone has less than 32GB of memory.\n"
+ ]
+ }
+ ],
+ "source": [
+ "print \"Let p=Vandana's smartphone has at least 32GB of memory.\"\n",
+ "print \"The negation of p is ( ~p ) :It is not the case that Vandana's smartphone has at least 32GB of memory.\"\n",
+ "print \"Or in simple English ( ~p ): Vandana's smartphone does not have at least 32GB of memory.\"\n",
+ "print \"Or even more simple as ( ~p ): Vandana's smartphone has less than 32GB of memory.\"\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 05:Page 04"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Let p,q be two propositions\n",
+ "Let p= Rebecca's PC has more than 16GB free hard disk space \n",
+ "Let q= The processor in Rebecca's PC runs faster than 1GHz\n",
+ "Conjunction of p^q is : Rebecca's PC has more than 16GB free hard disk space and The processor in Rebecca's PC runs faster than 1GHz\n"
+ ]
+ }
+ ],
+ "source": [
+ "p=\"Rebecca's PC has more than 16GB free hard disk space\"\n",
+ "q=\"The processor in Rebecca's PC runs faster than 1GHz\"\n",
+ "print \"Let p,q be two propositions\"\n",
+ "print \"Let p=\",p,\"\\n\",\"Let q=\",q\n",
+ "print \"Conjunction of p^q is : \"+p+\" and \"+q #conjunction combines two propositons with \"and\"\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 06:Page 05"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Let p,q be two propositions\n",
+ "Let p= Rebecca's PC has more than 16GB free hard disk space \n",
+ "Let q= The processor in Rebecca's PC runs faster than 1GHz\n",
+ "Disjunction of p\\/q is : Rebecca's PC has more than 16GB free hard disk space or The processor in Rebecca's PC runs faster than 1GHz\n"
+ ]
+ }
+ ],
+ "source": [
+ "p=\"Rebecca's PC has more than 16GB free hard disk space\"\n",
+ "q=\"The processor in Rebecca's PC runs faster than 1GHz\"\n",
+ "print \"Let p,q be two propositions\"\n",
+ "print \"Let p=\",p,\"\\n\",\"Let q=\",q\n",
+ "print \"Disjunction of p\\/q is : \"+p+\" or \"+q #unavailability of cup symbol. So \\/\n",
+ "#Disjunction combines two propositons using OR \n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 07:Page 07"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Let p= Maria learns discrete mathematics \n",
+ "Let q= Maria will find a good job\n",
+ "p->q is : If Maria learns discrete mathematics then Maria will find a good job\n",
+ "p->q is also expressed as : Maria will find a good job when Maria learns discrete mathematics\n"
+ ]
+ }
+ ],
+ "source": [
+ "p=\"Maria learns discrete mathematics\"\n",
+ "q=\"Maria will find a good job\"\n",
+ "print\"Let p=\",p,\"\\n\",\"Let q=\",q\n",
+ "print\"p->q is : \"+\"If \"+p+\" then \"+q #p->q p implies q means If P then Q.\n",
+ "print\"p->q is also expressed as :\",q,\" when \",p\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01:Page 37"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "p( 4 ) which is the statement 4 >3, is true\n",
+ "p( 2 ) which is the statement 2 >3, is false\n"
+ ]
+ }
+ ],
+ "source": [
+ "def p(x): #Function defined to check whether the given statements are true.\n",
+ " if(x>3):\n",
+ " print \"p(\",x,\") which is the statement\",x,\">3, is true\"\n",
+ " else:\n",
+ " print \"p(\",x,\") which is the statement\",x,\">3, is false\"\n",
+ "p(4)#Fuction call \n",
+ "p(2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 02:Page 38"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Systems under attack are CS1 and MATH1. The truth values for the same are calculated using functions.\n",
+ "A( CS1 ) is true.\n",
+ "A( CS2 ) is false.\n",
+ "A( MATH1 ) is true.\n"
+ ]
+ }
+ ],
+ "source": [
+ "x1=\"CS1\" #Defining systems to check whether they are under attack through a function.\n",
+ "x2=\"CS2\"\n",
+ "x3=\"MATH1\"\n",
+ "def A(x):\n",
+ " if(x==\"CS1\"): #Since cs1 and Math1 are the two computers under attack\n",
+ " print \"A(\",x,\") is true.\"\n",
+ " else:\n",
+ " if(x==\"MATH1\"): #Since CS1 and MATH1 are the two computers under attack\n",
+ " print \"A(\",x,\") is true.\"\n",
+ " else:\n",
+ " print\"A(\",x,\") is false.\"\n",
+ "print \"Systems under attack are CS1 and MATH1. The truth values for the same are calculated using functions.\"\n",
+ "A(x1)#Function call \n",
+ "A(x2)\n",
+ "A(x3)\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_(3)_1.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_(3)_1.ipynb
new file mode 100755
index 00000000..408d2fa2
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_(3)_1.ipynb
@@ -0,0 +1,394 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 01:Page 02"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The following sentences are Propositions\n",
+ "1. Washington D.C is the capital of the United States of America\n",
+ "2. Toronto is the capital of Canada\n",
+ "3. 1+1=2.\n",
+ "4. 2+2=3.\n"
+ ]
+ }
+ ],
+ "source": [
+ "print \"The following sentences are Propositions\" #Proposition should be a declarative sentence or should result in either a YES or a NO.\n",
+ "\n",
+ "print \"1. Washington D.C is the capital of the United States of America\\n2. Toronto is the capital of Canada\\n3. 1+1=2.\\n4. 2+2=3.\" #Since these statements are declarative and they answer the question YES or NO they are called propositions.\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 02:Page 02"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "1. What time is it? \n",
+ "2. Read this carefully. \n",
+ "3. x+1=2.\n",
+ "4. x+y=Z.\n",
+ "Sentences 1 and 2 are not propositions since they are not declarative. Sentences 3 and 4 are neither true nor false and so they are not propositions.\n"
+ ]
+ }
+ ],
+ "source": [
+ "print \"1. What time is it? \\n2. Read this carefully. \\n3. x+1=2.\\n4. x+y=Z.\"\n",
+ "print\"Sentences 1 and 2 are not propositions since they are not declarative. Sentences 3 and 4 are neither true nor false and so they are not propositions.\"\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 03:Page 03"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Propositon p=Michael's PC runs Linux.\n",
+ "\n",
+ " Negation of p is ~p : It is not the case that Michael's PC runs Linux.\n",
+ "\n",
+ " Negation of p is ~p : Michae's PC does not run.\n"
+ ]
+ }
+ ],
+ "source": [
+ "print \"Propositon p=Michael's PC runs Linux.\"\n",
+ "print \"\\n Negation of p is ~p : It is not the case that Michael's PC runs Linux.\"\n",
+ "print \"\\n Negation of p is ~p : Michae's PC does not run.\"#Negation is opposite of the truth value of the proposition expressed with \"it is not the case that\" or with \"not\".\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 04:Page 03"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Let p=Vandana's smartphone has at least 32GB of memory.\n",
+ "The negation of p is ( ~p ) :It is not the case that Vandana's smartphone has at least 32GB of memory.\n",
+ "Or in simple English ( ~p ): Vandana's smartphone does not have at least 32GB of memory.\n",
+ "Or even more simple as ( ~p ): Vandana's smartphone has less than 32GB of memory.\n"
+ ]
+ }
+ ],
+ "source": [
+ "print \"Let p=Vandana's smartphone has at least 32GB of memory.\"\n",
+ "print \"The negation of p is ( ~p ) :It is not the case that Vandana's smartphone has at least 32GB of memory.\"\n",
+ "print \"Or in simple English ( ~p ): Vandana's smartphone does not have at least 32GB of memory.\"\n",
+ "print \"Or even more simple as ( ~p ): Vandana's smartphone has less than 32GB of memory.\"\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 05:Page 04"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Let p,q be two propositions\n",
+ "Let p= Rebecca's PC has more than 16GB free hard disk space \n",
+ "Let q= The processor in Rebecca's PC runs faster than 1GHz\n",
+ "Conjunction of p^q is : Rebecca's PC has more than 16GB free hard disk space and The processor in Rebecca's PC runs faster than 1GHz\n"
+ ]
+ }
+ ],
+ "source": [
+ "p=\"Rebecca's PC has more than 16GB free hard disk space\"\n",
+ "q=\"The processor in Rebecca's PC runs faster than 1GHz\"\n",
+ "print \"Let p,q be two propositions\"\n",
+ "print \"Let p=\",p,\"\\n\",\"Let q=\",q\n",
+ "print \"Conjunction of p^q is : \"+p+\" and \"+q #conjunction combines two propositons with \"and\"\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 06:Page 05"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Let p,q be two propositions\n",
+ "Let p= Rebecca's PC has more than 16GB free hard disk space \n",
+ "Let q= The processor in Rebecca's PC runs faster than 1GHz\n",
+ "Disjunction of p\\/q is : Rebecca's PC has more than 16GB free hard disk space or The processor in Rebecca's PC runs faster than 1GHz\n"
+ ]
+ }
+ ],
+ "source": [
+ "p=\"Rebecca's PC has more than 16GB free hard disk space\"\n",
+ "q=\"The processor in Rebecca's PC runs faster than 1GHz\"\n",
+ "print \"Let p,q be two propositions\"\n",
+ "print \"Let p=\",p,\"\\n\",\"Let q=\",q\n",
+ "print \"Disjunction of p\\/q is : \"+p+\" or \"+q #unavailability of cup symbol. So \\/\n",
+ "#Disjunction combines two propositons using OR \n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 07:Page 07"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Let p= Maria learns discrete mathematics \n",
+ "Let q= Maria will find a good job\n",
+ "p->q is : If Maria learns discrete mathematics then Maria will find a good job\n",
+ "p->q is also expressed as : Maria will find a good job when Maria learns discrete mathematics\n"
+ ]
+ }
+ ],
+ "source": [
+ "p=\"Maria learns discrete mathematics\"\n",
+ "q=\"Maria will find a good job\"\n",
+ "print\"Let p=\",p,\"\\n\",\"Let q=\",q\n",
+ "print\"p->q is : \"+\"If \"+p+\" then \"+q #p->q p implies q means If P then Q.\n",
+ "print\"p->q is also expressed as :\",q,\" when \",p\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01:Page 37"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "p( 4 ) which is the statement 4 >3, is true\n",
+ "p( 2 ) which is the statement 2 >3, is false\n"
+ ]
+ }
+ ],
+ "source": [
+ "def p(x): #Function defined to check whether the given statements are true.\n",
+ " if(x>3):\n",
+ " print \"p(\",x,\") which is the statement\",x,\">3, is true\"\n",
+ " else:\n",
+ " print \"p(\",x,\") which is the statement\",x,\">3, is false\"\n",
+ "p(4)#Fuction call \n",
+ "p(2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 01:THE FOUNDATIONS: LOGIC AND PROOFS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 02:Page 38"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Systems under attack are CS1 and MATH1. The truth values for the same are calculated using functions.\n",
+ "A( CS1 ) is true.\n",
+ "A( CS2 ) is false.\n",
+ "A( MATH1 ) is true.\n"
+ ]
+ }
+ ],
+ "source": [
+ "x1=\"CS1\" #Defining systems to check whether they are under attack through a function.\n",
+ "x2=\"CS2\"\n",
+ "x3=\"MATH1\"\n",
+ "def A(x):\n",
+ " if(x==\"CS1\"): #Since cs1 and Math1 are the two computers under attack\n",
+ " print \"A(\",x,\") is true.\"\n",
+ " else:\n",
+ " if(x==\"MATH1\"): #Since CS1 and MATH1 are the two computers under attack\n",
+ " print \"A(\",x,\") is true.\"\n",
+ " else:\n",
+ " print\"A(\",x,\") is false.\"\n",
+ "print \"Systems under attack are CS1 and MATH1. The truth values for the same are calculated using functions.\"\n",
+ "A(x1)#Function call \n",
+ "A(x2)\n",
+ "A(x3)\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_1.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_1.ipynb
new file mode 100644
index 00000000..fe652a17
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter1_1.ipynb
@@ -0,0 +1 @@
+{"nbformat_minor": 0, "cells": [{"source": "#Chapter 01: The Foundations: Logic and Proofs\n", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 01:Page 02", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "print \"The following sentences are Propositions\" #Proposition should be a declarative sentence or should result in either a YES or a NO.\n\nprint \"1. Washington D.C is the capital of the United States of America\\n2. Toronto is the capital of Canada\\n3. 1+1=2.\\n4. 2+2=3.\" #Since these statements are declarative and they answer the question YES or NO they are called propositions.\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The following sentences are Propositions\n1. Washington D.C is the capital of the United States of America\n2. Toronto is the capital of Canada\n3. 1+1=2.\n4. 2+2=3.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 02:Page 02", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "print \"1. What time is it? \\n2. Read this carefully. \\n3. x+1=2.\\n4. x+y=Z.\"\nprint\"Sentences 1 and 2 are not propositions since they are not declarative. Sentences 3 and 4 are neither true nor false and so they are not propositions.\"\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "1. What time is it? \n2. Read this carefully. \n3. x+1=2.\n4. x+y=Z.\nSentences 1 and 2 are not propositions since they are not declarative. Sentences 3 and 4 are neither true nor false and so they are not propositions.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 03:Page 03", "cell_type": "markdown", "metadata": {}}, {"execution_count": 9, "cell_type": "code", "source": "print \"Propositon p=Michael's PC runs Linux.\"\nprint \"\\n Negation of p is ~p : It is not the case that Michael's PC runs Linux.\"\nprint \"\\n Negation of p is ~p : Michae's PC does not run.\"#Negation is opposite of the truth value of the proposition expressed with \"it is not the case that\" or with \"not\".\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Propositon p=Michael's PC runs Linux.\n\n Negation of p is ~p : It is not the case that Michael's PC runs Linux.\n\n Negation of p is ~p : Michae's PC does not run.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 04:Page 03", "cell_type": "markdown", "metadata": {}}, {"execution_count": 10, "cell_type": "code", "source": "print \"Let p=Vandana's smartphone has at least 32GB of memory.\"\nprint \"The negation of p is ( ~p ) :It is not the case that Vandana's smartphone has at least 32GB of memory.\"\nprint \"Or in simple English ( ~p ): Vandana's smartphone does not have at least 32GB of memory.\"\nprint \"Or even more simple as ( ~p ): Vandana's smartphone has less than 32GB of memory.\"\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Let p=Vandana's smartphone has at least 32GB of memory.\nThe negation of p is ( ~p ) :It is not the case that Vandana's smartphone has at least 32GB of memory.\nOr in simple English ( ~p ): Vandana's smartphone does not have at least 32GB of memory.\nOr even more simple as ( ~p ): Vandana's smartphone has less than 32GB of memory.\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 05:Page 04", "cell_type": "markdown", "metadata": {}}, {"execution_count": 11, "cell_type": "code", "source": "p=\"Rebecca's PC has more than 16GB free hard disk space\"\nq=\"The processor in Rebecca's PC runs faster than 1GHz\"\nprint \"Let p,q be two propositions\"\nprint \"Let p=\",p,\"\\n\",\"Let q=\",q\nprint \"Conjunction of p^q is : \"+p+\" and \"+q #conjunction combines two propositons with \"and\"\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Let p,q be two propositions\nLet p= Rebecca's PC has more than 16GB free hard disk space \nLet q= The processor in Rebecca's PC runs faster than 1GHz\nConjunction of p^q is : Rebecca's PC has more than 16GB free hard disk space and The processor in Rebecca's PC runs faster than 1GHz\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 06:Page 05", "cell_type": "markdown", "metadata": {}}, {"execution_count": 12, "cell_type": "code", "source": "p=\"Rebecca's PC has more than 16GB free hard disk space\"\nq=\"The processor in Rebecca's PC runs faster than 1GHz\"\nprint \"Let p,q be two propositions\"\nprint \"Let p=\",p,\"\\n\",\"Let q=\",q\nprint \"Disjunction of p\\/q is : \"+p+\" or \"+q #unavailability of cup symbol. So \\/\n#Disjunction combines two propositons using OR \n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Let p,q be two propositions\nLet p= Rebecca's PC has more than 16GB free hard disk space \nLet q= The processor in Rebecca's PC runs faster than 1GHz\nDisjunction of p\\/q is : Rebecca's PC has more than 16GB free hard disk space or The processor in Rebecca's PC runs faster than 1GHz\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 07:Page 07", "cell_type": "markdown", "metadata": {}}, {"execution_count": 14, "cell_type": "code", "source": "p=\"Maria learns discrete mathematics\"\nq=\"Maria will find a good job\"\nprint\"Let p=\",p,\"\\n\",\"Let q=\",q\nprint\"p->q is : \"+\"If \"+p+\" then \"+q #p->q p implies q means If P then Q.\nprint\"p->q is also expressed as :\",q,\" when \",p\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Let p= Maria learns discrete mathematics \nLet q= Maria will find a good job\np->q is : If Maria learns discrete mathematics then Maria will find a good job\np->q is also expressed as : Maria will find a good job when Maria learns discrete mathematics\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {"collapsed": true}}, {"source": "## Example 01:Page 37", "cell_type": "markdown", "metadata": {}}, {"execution_count": 15, "cell_type": "code", "source": "def p(x): #Function defined to check whether the given statements are true.\n if(x>3):\n print \"p(\",x,\") which is the statement\",x,\">3, is true\"\n else:\n print \"p(\",x,\") which is the statement\",x,\">3, is false\"\np(4)#Fuction call \np(2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "p( 4 ) which is the statement 4 >3, is true\np( 2 ) which is the statement 2 >3, is false\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 01: The Foundations: Logic and Proofs", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 02:Page 38", "cell_type": "markdown", "metadata": {}}, {"execution_count": 17, "cell_type": "code", "source": "x1=\"CS1\" #Defining systems to check whether they are under attack through a function.\nx2=\"CS2\"\nx3=\"MATH1\"\ndef A(x):\n if(x==\"CS1\"): #Since cs1 and Math1 are the two computers under attack\n print \"A(\",x,\") is true.\"\n else:\n if(x==\"MATH1\"): #Since CS1 and MATH1 are the two computers under attack\n print \"A(\",x,\") is true.\"\n else:\n print\"A(\",x,\") is false.\"\nprint \"Systems under attack are CS1 and MATH1. The truth values for the same are calculated using functions.\"\nA(x1)#Function call \nA(x2)\nA(x3)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Systems under attack are CS1 and MATH1. The truth values for the same are calculated using functions.\nA( CS1 ) is true.\nA( CS2 ) is false.\nA( MATH1 ) is true.\n"}], "metadata": {"collapsed": false, "trusted": true}}], "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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3.ipynb
new file mode 100755
index 00000000..251e4767
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3.ipynb
@@ -0,0 +1,144 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 03:ALGORITHMS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Found number 19 at the position 12\n"
+ ]
+ }
+ ],
+ "source": [
+ "def binarysearch(a,num): #function definition with its parameters 'a' is the inputlist\n",
+ " #and 'num' number to be found\n",
+ "\n",
+ " first=0 #initially the first position is zero\n",
+ " last=len(a)-1 #initially the last position is the total length of the inputlist-1\n",
+ " found=False #boolean value to indicate if the number to be searched is found or not.\n",
+ "\n",
+ " while first<=last and not found:\n",
+ " midpoint=(first+last)//2 #dividing the inputlist into two halves and comparing the number to be found with the midpoint.\n",
+ "\n",
+ " if a[midpoint]==num: #If the number to be found is equal to the midpoint returns the position.\n",
+ " found=True\n",
+ " else:\n",
+ " if num<a[midpoint]: #if the number to be found is less than the midpoint\n",
+ " #then the first half of the divided input list is taken for further computation.\n",
+ "\n",
+ " last=midpoint-1 #by assigning the last number of the first half(number before the midpoint) to the variable last.\n",
+ "\n",
+ "\n",
+ " else:\n",
+ " first=midpoint+1 #if the number to be found is greater than the midpoint\n",
+ " #then the second half of the divided input list is taken for further computation.\n",
+ " #by assigning the first number of the second half(number following the midpoint) to the variable first.\n",
+ "\n",
+ " return midpoint #returns the position of the number found in the list. \n",
+ "\n",
+ "\n",
+ "\n",
+ "numlist=[1, 23, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22] #List of inputs\n",
+ "print \"Found number 19 at the position\",(binarysearch(numlist, 19)) #Printing the position of the number to be found by a function call.\n",
+ " #The function binarysearch is called along with its parameters, inputlist\n",
+ " #and the number to be found.\n",
+ "\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 03:ALGORITHMS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 05: Page 198"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[2, 3, 4, 1, 5]\n",
+ "[1, 2, 3, 4, 5]\n",
+ "[1, 2, 3, 4, 5]\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To perform insertionsort\n",
+ "def sort_insertion(inputlist):\n",
+ "\n",
+ " for i in range(1,len(inputlist)):\n",
+ "\n",
+ " val_current = inputlist[i]\n",
+ " pos = i \n",
+ " \n",
+ " # check backwards through sorted list for proper pos of val_current\n",
+ " while((pos > 0) and (inputlist[pos-1] > val_current)):\n",
+ " inputlist[pos] = inputlist[pos-1]\n",
+ " pos = pos-1\n",
+ " \n",
+ " if pos != i:\n",
+ " inputlist[pos] = val_current \n",
+ " print(inputlist)\n",
+ " return inputlist\n",
+ "inputlist = [3,2,4,1,5]\n",
+ "print sort_insertion(inputlist)\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_1.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_1.ipynb
new file mode 100755
index 00000000..251e4767
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_1.ipynb
@@ -0,0 +1,144 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 03:ALGORITHMS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 195"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Found number 19 at the position 12\n"
+ ]
+ }
+ ],
+ "source": [
+ "def binarysearch(a,num): #function definition with its parameters 'a' is the inputlist\n",
+ " #and 'num' number to be found\n",
+ "\n",
+ " first=0 #initially the first position is zero\n",
+ " last=len(a)-1 #initially the last position is the total length of the inputlist-1\n",
+ " found=False #boolean value to indicate if the number to be searched is found or not.\n",
+ "\n",
+ " while first<=last and not found:\n",
+ " midpoint=(first+last)//2 #dividing the inputlist into two halves and comparing the number to be found with the midpoint.\n",
+ "\n",
+ " if a[midpoint]==num: #If the number to be found is equal to the midpoint returns the position.\n",
+ " found=True\n",
+ " else:\n",
+ " if num<a[midpoint]: #if the number to be found is less than the midpoint\n",
+ " #then the first half of the divided input list is taken for further computation.\n",
+ "\n",
+ " last=midpoint-1 #by assigning the last number of the first half(number before the midpoint) to the variable last.\n",
+ "\n",
+ "\n",
+ " else:\n",
+ " first=midpoint+1 #if the number to be found is greater than the midpoint\n",
+ " #then the second half of the divided input list is taken for further computation.\n",
+ " #by assigning the first number of the second half(number following the midpoint) to the variable first.\n",
+ "\n",
+ " return midpoint #returns the position of the number found in the list. \n",
+ "\n",
+ "\n",
+ "\n",
+ "numlist=[1, 23, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22] #List of inputs\n",
+ "print \"Found number 19 at the position\",(binarysearch(numlist, 19)) #Printing the position of the number to be found by a function call.\n",
+ " #The function binarysearch is called along with its parameters, inputlist\n",
+ " #and the number to be found.\n",
+ "\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 03:ALGORITHMS"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Example 05: Page 198"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[2, 3, 4, 1, 5]\n",
+ "[1, 2, 3, 4, 5]\n",
+ "[1, 2, 3, 4, 5]\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To perform insertionsort\n",
+ "def sort_insertion(inputlist):\n",
+ "\n",
+ " for i in range(1,len(inputlist)):\n",
+ "\n",
+ " val_current = inputlist[i]\n",
+ " pos = i \n",
+ " \n",
+ " # check backwards through sorted list for proper pos of val_current\n",
+ " while((pos > 0) and (inputlist[pos-1] > val_current)):\n",
+ " inputlist[pos] = inputlist[pos-1]\n",
+ " pos = pos-1\n",
+ " \n",
+ " if pos != i:\n",
+ " inputlist[pos] = val_current \n",
+ " print(inputlist)\n",
+ " return inputlist\n",
+ "inputlist = [3,2,4,1,5]\n",
+ "print sort_insertion(inputlist)\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_2.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_2.ipynb
new file mode 100644
index 00000000..e1c0050e
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_2.ipynb
@@ -0,0 +1 @@
+{"nbformat_minor": 0, "cells": [{"source": "#Chapter 03: Algorithms", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 195", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "def binarysearch(a,num): #function definition with its parameters 'a' is the inputlist\n #and 'num' number to be found\n\n first=0 #initially the first position is zero\n last=len(a)-1 #initially the last position is the total length of the inputlist-1\n found=False #boolean value to indicate if the number to be searched is found or not.\n\n while first<=last and not found:\n midpoint=(first+last)//2 #dividing the inputlist into two halves and comparing the number to be found with the midpoint.\n\n if a[midpoint]==num: #If the number to be found is equal to the midpoint returns the position.\n found=True\n else:\n if num<a[midpoint]: #if the number to be found is less than the midpoint\n #then the first half of the divided input list is taken for further computation.\n\n last=midpoint-1 #by assigning the last number of the first half(number before the midpoint) to the variable last.\n\n\n else:\n first=midpoint+1 #if the number to be found is greater than the midpoint\n #then the second half of the divided input list is taken for further computation.\n #by assigning the first number of the second half(number following the midpoint) to the variable first.\n\n return midpoint #returns the position of the number found in the list. \n\n\n\nnumlist=[1, 23, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22] #List of inputs\nprint \"Found number 19 at the position\",(binarysearch(numlist, 19)) #Printing the position of the number to be found by a function call.\n #The function binarysearch is called along with its parameters, inputlist\n #and the number to be found.\n\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Found number 19 at the position 12\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 03: Algorithms", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 05: Page 198", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "#To perform insertionsort\ndef sort_insertion(inputlist):\n\n for i in range(1,len(inputlist)):\n\n val_current = inputlist[i]\n pos = i \n \n # check backwards through sorted list for proper pos of val_current\n while((pos > 0) and (inputlist[pos-1] > val_current)):\n inputlist[pos] = inputlist[pos-1]\n pos = pos-1\n \n if pos != i:\n inputlist[pos] = val_current \n print(inputlist)\n return inputlist\ninputlist = [3,2,4,1,5]\nprint sort_insertion(inputlist)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "[2, 3, 4, 1, 5]\n[1, 2, 3, 4, 5]\n[1, 2, 3, 4, 5]\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "", "cell_type": "markdown", "metadata": {}}, {"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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_3.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_3.ipynb
new file mode 100644
index 00000000..e1c0050e
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter3_3.ipynb
@@ -0,0 +1 @@
+{"nbformat_minor": 0, "cells": [{"source": "#Chapter 03: Algorithms", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 195", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "def binarysearch(a,num): #function definition with its parameters 'a' is the inputlist\n #and 'num' number to be found\n\n first=0 #initially the first position is zero\n last=len(a)-1 #initially the last position is the total length of the inputlist-1\n found=False #boolean value to indicate if the number to be searched is found or not.\n\n while first<=last and not found:\n midpoint=(first+last)//2 #dividing the inputlist into two halves and comparing the number to be found with the midpoint.\n\n if a[midpoint]==num: #If the number to be found is equal to the midpoint returns the position.\n found=True\n else:\n if num<a[midpoint]: #if the number to be found is less than the midpoint\n #then the first half of the divided input list is taken for further computation.\n\n last=midpoint-1 #by assigning the last number of the first half(number before the midpoint) to the variable last.\n\n\n else:\n first=midpoint+1 #if the number to be found is greater than the midpoint\n #then the second half of the divided input list is taken for further computation.\n #by assigning the first number of the second half(number following the midpoint) to the variable first.\n\n return midpoint #returns the position of the number found in the list. \n\n\n\nnumlist=[1, 23, 5, 6, 7, 8, 10, 12, 13, 15, 16, 18, 19, 20, 22] #List of inputs\nprint \"Found number 19 at the position\",(binarysearch(numlist, 19)) #Printing the position of the number to be found by a function call.\n #The function binarysearch is called along with its parameters, inputlist\n #and the number to be found.\n\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Found number 19 at the position 12\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 03: Algorithms", "cell_type": "markdown", "metadata": {}}, {"source": "##Example 05: Page 198", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "#To perform insertionsort\ndef sort_insertion(inputlist):\n\n for i in range(1,len(inputlist)):\n\n val_current = inputlist[i]\n pos = i \n \n # check backwards through sorted list for proper pos of val_current\n while((pos > 0) and (inputlist[pos-1] > val_current)):\n inputlist[pos] = inputlist[pos-1]\n pos = pos-1\n \n if pos != i:\n inputlist[pos] = val_current \n print(inputlist)\n return inputlist\ninputlist = [3,2,4,1,5]\nprint sort_insertion(inputlist)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "[2, 3, 4, 1, 5]\n[1, 2, 3, 4, 5]\n[1, 2, 3, 4, 5]\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "", "cell_type": "markdown", "metadata": {}}, {"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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4.ipynb
new file mode 100755
index 00000000..91c1e06d
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4.ipynb
@@ -0,0 +1,743 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 239"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The quotient when 101 is divided by 11 is 9 = 101 div 11 and the remainder is 2 = 101 mod 11\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To find the quotient and remainder \n",
+ "dividend=101\n",
+ "divisor=11\n",
+ "quotient=dividend/divisor #To find quotient\n",
+ "remainder=dividend%divisor #To find remainder\n",
+ "dividend=(divisor*quotient)+remainder\n",
+ "print \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 240"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The quotient when -11 is divided by 3 is -4 = -11 div 3 and the remainder is 1 = -11 mod 3\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To find the quotient and remainder\n",
+ "dividend=-11\n",
+ "divisor=3\n",
+ "quotient=dividend/divisor\n",
+ "remainder=dividend%divisor\n",
+ "dividend=(divisor*quotient)+remainder\n",
+ "print \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 246"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "enter a number: 101011111\n",
+ "351\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To convert binary to decimal equivalent\n",
+ "binary_num= raw_input('enter a number: ')\n",
+ "decimal = 0\n",
+ "for digit in binary_num:\n",
+ " decimal = decimal*2 + int(digit)\n",
+ "\n",
+ "print decimal\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 247"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false,
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "enter a number: 2AE0B\n",
+ "The conversion of 2AE0B to hexadeimal is 175627\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To convert decimal to hexadecimal\n",
+ "dec= raw_input('enter a number: ')\n",
+ "\n",
+ "print \"The conversion of\",dec,\"to hexadeimal is\",int(dec,16)\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 247"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter a number12345\n",
+ "The decimal number 12345 is converted to its octal equivalent : 3 0 0 7 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute decimal to octal\n",
+ "numbers= []\n",
+ "dec=input(\"Enter a number\");\n",
+ "num=dec\n",
+ "while dec!=0:\n",
+ " \n",
+ " rem=dec%8\n",
+ " dec=dec/8\n",
+ " numbers.append(rem)\n",
+ "print \"The decimal number\",num,\"is converted to its octal equivalent : \",\n",
+ "for i in reversed(numbers):\n",
+ " print i,\n",
+ " \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 05: Page 248"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the decimal number which is to be converted to hexadecimal177130\n",
+ "The hexadecimal equivalent of decimal 177130 is 0 2 B 3 E A\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To convert Decimal to hexadecimal\n",
+ "num=[]\n",
+ "def ChangeHex(n): #function to convert\n",
+ " if (n < 0):\n",
+ " num.append(\"\")\n",
+ " elif (n<=1):\n",
+ " num.append(n)\n",
+ " else: #for numbers greater than 9\n",
+ " x =(n%16)\n",
+ " if (x < 10):\n",
+ " num.append(x) \n",
+ " if (x == 10):\n",
+ " num.append(\"A\")\n",
+ " if (x == 11):\n",
+ " num.append(\"B\")\n",
+ " if (x == 12):\n",
+ " num.append(\"C\")\n",
+ " if (x == 13):\n",
+ " num.append(\"D\")\n",
+ " if (x == 14):\n",
+ " num.append(\"E\")\n",
+ " if (x == 15):\n",
+ " num.append(\"F\")\n",
+ " ChangeHex( n / 16 )\n",
+ "dec_num=input(\"Enter the decimal number which is to be converted to hexadecimal\");\n",
+ "ChangeHex(dec_num)\n",
+ "print \"The hexadecimal equivalent of decimal\",dec_num,\"is\",\n",
+ "for i in reversed(num):\n",
+ " print i,\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 06: Page 249"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter a decimal number241\n",
+ "\n",
+ "The binary equivalent of decimal 241 is 1 1 1 1 0 0 0 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Compute Decimal to Binary\n",
+ "array=[]\n",
+ "def conv(n):\n",
+ " if n==0:\n",
+ " print ''\n",
+ " else:\n",
+ " array.append(str(n%2)) #to compute remainder and append it to the result\n",
+ " return conv(n/2) \n",
+ "dec_num=input(\"Enter a decimal number\")\n",
+ "conv(dec_num)\n",
+ "print \"The binary equivalent of decimal\",dec_num,\"is\",\n",
+ "for i in reversed(array):\n",
+ " print i,\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 06: Page 249"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "enter the first number: 1110\n",
+ "enter the second number: 1011\n",
+ "The sum of binary numbers 1110 and 1011 is 11001\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute the binary addition\n",
+ "def binAdd(bin1, bin2): #function to add two binary numbers\n",
+ " if not bin1 or not bin2:#checks if both the numbers are binary\n",
+ " return '' \n",
+ "\n",
+ " maxlen = max(len(bin1), len(bin2))\n",
+ "\n",
+ " bin1 = bin1.zfill(maxlen) #zfill fills with zero to fill the entire width\n",
+ " bin2 = bin2.zfill(maxlen)\n",
+ "\n",
+ " result = ''\n",
+ " carry = 0\n",
+ "\n",
+ " i = maxlen - 1\n",
+ " while(i >= 0):\n",
+ " s = int(bin1[i]) + int(bin2[i])#adding bit by bit\n",
+ " if s == 2: #1+1\n",
+ " if carry == 0:\n",
+ " carry = 1\n",
+ " result = \"%s%s\" % (result, '0')\n",
+ " else:\n",
+ " result = \"%s%s\" % (result, '1')\n",
+ " elif s == 1: # 1+0\n",
+ " if carry == 1:\n",
+ " result = \"%s%s\" % (result, '0')\n",
+ " else:\n",
+ " result = \"%s%s\" % (result, '1')\n",
+ " else: # 0+0\n",
+ " if carry == 1:\n",
+ " result = \"%s%s\" % (result, '1')\n",
+ " carry = 0 \n",
+ " else:\n",
+ " result = \"%s%s\" % (result, '0') \n",
+ "\n",
+ " i = i - 1;\n",
+ "\n",
+ " if carry>0:\n",
+ " result = \"%s%s\" % (result, '1')\n",
+ " return result[::-1]\n",
+ "bin1 = raw_input('enter the first number: ')\n",
+ "bin2 = raw_input('enter the second number: ')\n",
+ "print \"The sum of binary numbers\",bin1,\"and\",bin2,\"is\",binAdd(bin1,bin2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 02: Page 258"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number for which the prime factors have to be found100\n",
+ "[2, 2, 5, 5]\n"
+ ]
+ }
+ ],
+ "source": [
+ "#to find the prime factors\n",
+ "\n",
+ "def prime_factors(n):\n",
+ " i = 2\n",
+ " factors = []\n",
+ " while i * i <= n:\n",
+ " if n % i: #modulp division to check of the number is prime or not\n",
+ " i += 1\n",
+ " else:\n",
+ " n //= i\n",
+ " factors.append(i) #append those numbers which readily divides the given number\n",
+ " if n > 1:\n",
+ " factors.append(n)\n",
+ " return factors\n",
+ "number=input(\"Enter the number for which the prime factors have to be found\");\n",
+ "a=prime_factors(number)\n",
+ "print a\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 258"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number101\n",
+ "101 is prime\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To say if a number is prime or not\n",
+ "globals() ['count']=0\n",
+ "n=input(\"Enter the number\");\n",
+ "for i in range(2,n):#number thats not divisible by other than one and itself. so from 2 to n (n-1 in python for loop)\n",
+ " if n%i==0:\n",
+ " count=count+1\n",
+ " num=i\n",
+ "if count==0:\n",
+ " print n,\"is prime\" \n",
+ "else:\n",
+ " print n,\"is not prime because its divisible by\",num\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 259"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number for which the prime factors have to be found7007\n",
+ "[7, 7, 11, 13]\n"
+ ]
+ }
+ ],
+ "source": [
+ "#to find the prime factors\n",
+ "\n",
+ "def prime_factors(n):\n",
+ " i = 2\n",
+ " factors = []\n",
+ " while i * i <= n:\n",
+ " if n % i: #modulp division to check of the number is prime or not\n",
+ " i += 1\n",
+ " else:\n",
+ " n //= i\n",
+ " factors.append(i) #append those numbers which readily divides the given number\n",
+ " if n > 1:\n",
+ " factors.append(n)\n",
+ " return factors\n",
+ "number=input(\"Enter the number for which the prime factors have to be found\");\n",
+ "a=prime_factors(number)\n",
+ "print a\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 10: Page 263"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 24,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the first number24\n",
+ "Enter the second number36\n",
+ "GCD( 24 , 36 ) is 12\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute GCD\n",
+ "def gcd(a,b):#fuction computes gcd\n",
+ " if b > a:\n",
+ " return gcd(b,a)\n",
+ " r = a%b\n",
+ " if r == 0:\n",
+ " return b\n",
+ " return gcd(r,b)\n",
+ "n1=input(\"Enter the first number\");\n",
+ "n2=input(\"Enter the second number\");\n",
+ "print \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 11: Page 263"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the first number17\n",
+ "Enter the second number22\n",
+ "GCD( 17 , 22 ) is 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute GCD\n",
+ "def gcd(a,b):#fuction computes gcd\n",
+ " if b > a:\n",
+ " return gcd(b,a)\n",
+ " r = a%b\n",
+ " if r == 0:\n",
+ " return b\n",
+ " return gcd(r,b)\n",
+ "n1=input(\"Enter the first number\");\n",
+ "n2=input(\"Enter the second number\");\n",
+ "print \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 16: Page 268"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the first number414\n",
+ "Enter the second number662\n",
+ "gcd( 414 , 662 )is 2\n"
+ ]
+ }
+ ],
+ "source": [
+ "#to find gcd using euclidean algorithm\n",
+ "def gcd(a,b):#euclidean algithm definition\n",
+ " x=a\n",
+ " y=b\n",
+ " while y!=0:\n",
+ " r=x%y\n",
+ " x=y\n",
+ " y=r\n",
+ " print \"gcd(\",a,\",\",b,\")is\",x\n",
+ "num1=input(\"Enter the first number\");\n",
+ "num2=input(\"Enter the second number\");\n",
+ "gcd(num1,num2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 17: Page 270"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the first number252\n",
+ "Enter the second number198\n",
+ "gcd( 252 , 198 )is 18\n"
+ ]
+ }
+ ],
+ "source": [
+ "#to find gcd using euclidean algorithm\n",
+ "def gcd(a,b):#euclidean algithm definition\n",
+ " x=a\n",
+ " y=b\n",
+ " while y!=0:\n",
+ " r=x%y\n",
+ " x=y\n",
+ " y=r\n",
+ " print \"gcd(\",a,\",\",b,\")is\",x\n",
+ "num1=input(\"Enter the first number\");\n",
+ "num2=input(\"Enter the second number\");\n",
+ "gcd(num1,num2)\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_1.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_1.ipynb
new file mode 100755
index 00000000..91c1e06d
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_1.ipynb
@@ -0,0 +1,743 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 239"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The quotient when 101 is divided by 11 is 9 = 101 div 11 and the remainder is 2 = 101 mod 11\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To find the quotient and remainder \n",
+ "dividend=101\n",
+ "divisor=11\n",
+ "quotient=dividend/divisor #To find quotient\n",
+ "remainder=dividend%divisor #To find remainder\n",
+ "dividend=(divisor*quotient)+remainder\n",
+ "print \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 240"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The quotient when -11 is divided by 3 is -4 = -11 div 3 and the remainder is 1 = -11 mod 3\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To find the quotient and remainder\n",
+ "dividend=-11\n",
+ "divisor=3\n",
+ "quotient=dividend/divisor\n",
+ "remainder=dividend%divisor\n",
+ "dividend=(divisor*quotient)+remainder\n",
+ "print \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 246"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "enter a number: 101011111\n",
+ "351\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To convert binary to decimal equivalent\n",
+ "binary_num= raw_input('enter a number: ')\n",
+ "decimal = 0\n",
+ "for digit in binary_num:\n",
+ " decimal = decimal*2 + int(digit)\n",
+ "\n",
+ "print decimal\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 247"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false,
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "enter a number: 2AE0B\n",
+ "The conversion of 2AE0B to hexadeimal is 175627\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To convert decimal to hexadecimal\n",
+ "dec= raw_input('enter a number: ')\n",
+ "\n",
+ "print \"The conversion of\",dec,\"to hexadeimal is\",int(dec,16)\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 247"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter a number12345\n",
+ "The decimal number 12345 is converted to its octal equivalent : 3 0 0 7 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute decimal to octal\n",
+ "numbers= []\n",
+ "dec=input(\"Enter a number\");\n",
+ "num=dec\n",
+ "while dec!=0:\n",
+ " \n",
+ " rem=dec%8\n",
+ " dec=dec/8\n",
+ " numbers.append(rem)\n",
+ "print \"The decimal number\",num,\"is converted to its octal equivalent : \",\n",
+ "for i in reversed(numbers):\n",
+ " print i,\n",
+ " \n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 05: Page 248"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the decimal number which is to be converted to hexadecimal177130\n",
+ "The hexadecimal equivalent of decimal 177130 is 0 2 B 3 E A\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To convert Decimal to hexadecimal\n",
+ "num=[]\n",
+ "def ChangeHex(n): #function to convert\n",
+ " if (n < 0):\n",
+ " num.append(\"\")\n",
+ " elif (n<=1):\n",
+ " num.append(n)\n",
+ " else: #for numbers greater than 9\n",
+ " x =(n%16)\n",
+ " if (x < 10):\n",
+ " num.append(x) \n",
+ " if (x == 10):\n",
+ " num.append(\"A\")\n",
+ " if (x == 11):\n",
+ " num.append(\"B\")\n",
+ " if (x == 12):\n",
+ " num.append(\"C\")\n",
+ " if (x == 13):\n",
+ " num.append(\"D\")\n",
+ " if (x == 14):\n",
+ " num.append(\"E\")\n",
+ " if (x == 15):\n",
+ " num.append(\"F\")\n",
+ " ChangeHex( n / 16 )\n",
+ "dec_num=input(\"Enter the decimal number which is to be converted to hexadecimal\");\n",
+ "ChangeHex(dec_num)\n",
+ "print \"The hexadecimal equivalent of decimal\",dec_num,\"is\",\n",
+ "for i in reversed(num):\n",
+ " print i,\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 06: Page 249"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter a decimal number241\n",
+ "\n",
+ "The binary equivalent of decimal 241 is 1 1 1 1 0 0 0 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "#Compute Decimal to Binary\n",
+ "array=[]\n",
+ "def conv(n):\n",
+ " if n==0:\n",
+ " print ''\n",
+ " else:\n",
+ " array.append(str(n%2)) #to compute remainder and append it to the result\n",
+ " return conv(n/2) \n",
+ "dec_num=input(\"Enter a decimal number\")\n",
+ "conv(dec_num)\n",
+ "print \"The binary equivalent of decimal\",dec_num,\"is\",\n",
+ "for i in reversed(array):\n",
+ " print i,\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 06: Page 249"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "enter the first number: 1110\n",
+ "enter the second number: 1011\n",
+ "The sum of binary numbers 1110 and 1011 is 11001\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute the binary addition\n",
+ "def binAdd(bin1, bin2): #function to add two binary numbers\n",
+ " if not bin1 or not bin2:#checks if both the numbers are binary\n",
+ " return '' \n",
+ "\n",
+ " maxlen = max(len(bin1), len(bin2))\n",
+ "\n",
+ " bin1 = bin1.zfill(maxlen) #zfill fills with zero to fill the entire width\n",
+ " bin2 = bin2.zfill(maxlen)\n",
+ "\n",
+ " result = ''\n",
+ " carry = 0\n",
+ "\n",
+ " i = maxlen - 1\n",
+ " while(i >= 0):\n",
+ " s = int(bin1[i]) + int(bin2[i])#adding bit by bit\n",
+ " if s == 2: #1+1\n",
+ " if carry == 0:\n",
+ " carry = 1\n",
+ " result = \"%s%s\" % (result, '0')\n",
+ " else:\n",
+ " result = \"%s%s\" % (result, '1')\n",
+ " elif s == 1: # 1+0\n",
+ " if carry == 1:\n",
+ " result = \"%s%s\" % (result, '0')\n",
+ " else:\n",
+ " result = \"%s%s\" % (result, '1')\n",
+ " else: # 0+0\n",
+ " if carry == 1:\n",
+ " result = \"%s%s\" % (result, '1')\n",
+ " carry = 0 \n",
+ " else:\n",
+ " result = \"%s%s\" % (result, '0') \n",
+ "\n",
+ " i = i - 1;\n",
+ "\n",
+ " if carry>0:\n",
+ " result = \"%s%s\" % (result, '1')\n",
+ " return result[::-1]\n",
+ "bin1 = raw_input('enter the first number: ')\n",
+ "bin2 = raw_input('enter the second number: ')\n",
+ "print \"The sum of binary numbers\",bin1,\"and\",bin2,\"is\",binAdd(bin1,bin2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 02: Page 258"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number for which the prime factors have to be found100\n",
+ "[2, 2, 5, 5]\n"
+ ]
+ }
+ ],
+ "source": [
+ "#to find the prime factors\n",
+ "\n",
+ "def prime_factors(n):\n",
+ " i = 2\n",
+ " factors = []\n",
+ " while i * i <= n:\n",
+ " if n % i: #modulp division to check of the number is prime or not\n",
+ " i += 1\n",
+ " else:\n",
+ " n //= i\n",
+ " factors.append(i) #append those numbers which readily divides the given number\n",
+ " if n > 1:\n",
+ " factors.append(n)\n",
+ " return factors\n",
+ "number=input(\"Enter the number for which the prime factors have to be found\");\n",
+ "a=prime_factors(number)\n",
+ "print a\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 258"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number101\n",
+ "101 is prime\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To say if a number is prime or not\n",
+ "globals() ['count']=0\n",
+ "n=input(\"Enter the number\");\n",
+ "for i in range(2,n):#number thats not divisible by other than one and itself. so from 2 to n (n-1 in python for loop)\n",
+ " if n%i==0:\n",
+ " count=count+1\n",
+ " num=i\n",
+ "if count==0:\n",
+ " print n,\"is prime\" \n",
+ "else:\n",
+ " print n,\"is not prime because its divisible by\",num\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 259"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number for which the prime factors have to be found7007\n",
+ "[7, 7, 11, 13]\n"
+ ]
+ }
+ ],
+ "source": [
+ "#to find the prime factors\n",
+ "\n",
+ "def prime_factors(n):\n",
+ " i = 2\n",
+ " factors = []\n",
+ " while i * i <= n:\n",
+ " if n % i: #modulp division to check of the number is prime or not\n",
+ " i += 1\n",
+ " else:\n",
+ " n //= i\n",
+ " factors.append(i) #append those numbers which readily divides the given number\n",
+ " if n > 1:\n",
+ " factors.append(n)\n",
+ " return factors\n",
+ "number=input(\"Enter the number for which the prime factors have to be found\");\n",
+ "a=prime_factors(number)\n",
+ "print a\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 10: Page 263"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 24,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the first number24\n",
+ "Enter the second number36\n",
+ "GCD( 24 , 36 ) is 12\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute GCD\n",
+ "def gcd(a,b):#fuction computes gcd\n",
+ " if b > a:\n",
+ " return gcd(b,a)\n",
+ " r = a%b\n",
+ " if r == 0:\n",
+ " return b\n",
+ " return gcd(r,b)\n",
+ "n1=input(\"Enter the first number\");\n",
+ "n2=input(\"Enter the second number\");\n",
+ "print \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 11: Page 263"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the first number17\n",
+ "Enter the second number22\n",
+ "GCD( 17 , 22 ) is 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute GCD\n",
+ "def gcd(a,b):#fuction computes gcd\n",
+ " if b > a:\n",
+ " return gcd(b,a)\n",
+ " r = a%b\n",
+ " if r == 0:\n",
+ " return b\n",
+ " return gcd(r,b)\n",
+ "n1=input(\"Enter the first number\");\n",
+ "n2=input(\"Enter the second number\");\n",
+ "print \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 16: Page 268"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 26,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the first number414\n",
+ "Enter the second number662\n",
+ "gcd( 414 , 662 )is 2\n"
+ ]
+ }
+ ],
+ "source": [
+ "#to find gcd using euclidean algorithm\n",
+ "def gcd(a,b):#euclidean algithm definition\n",
+ " x=a\n",
+ " y=b\n",
+ " while y!=0:\n",
+ " r=x%y\n",
+ " x=y\n",
+ " y=r\n",
+ " print \"gcd(\",a,\",\",b,\")is\",x\n",
+ "num1=input(\"Enter the first number\");\n",
+ "num2=input(\"Enter the second number\");\n",
+ "gcd(num1,num2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "collapsed": true
+ },
+ "source": [
+ "# 04:NUMBER THEORY AND CRYPTOGRAPHY"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 17: Page 270"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the first number252\n",
+ "Enter the second number198\n",
+ "gcd( 252 , 198 )is 18\n"
+ ]
+ }
+ ],
+ "source": [
+ "#to find gcd using euclidean algorithm\n",
+ "def gcd(a,b):#euclidean algithm definition\n",
+ " x=a\n",
+ " y=b\n",
+ " while y!=0:\n",
+ " r=x%y\n",
+ " x=y\n",
+ " y=r\n",
+ " print \"gcd(\",a,\",\",b,\")is\",x\n",
+ "num1=input(\"Enter the first number\");\n",
+ "num2=input(\"Enter the second number\");\n",
+ "gcd(num1,num2)\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_2.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_2.ipynb
new file mode 100644
index 00000000..074d0a2b
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_2.ipynb
@@ -0,0 +1 @@
+{"nbformat_minor": 0, "cells": [{"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 239", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "#To find the quotient and remainder \ndividend=101\ndivisor=11\nquotient=dividend/divisor #To find quotient\nremainder=dividend%divisor #To find remainder\ndividend=(divisor*quotient)+remainder\nprint \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The quotient when 101 is divided by 11 is 9 = 101 div 11 and the remainder is 2 = 101 mod 11\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 240", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "#To find the quotient and remainder\ndividend=-11\ndivisor=3\nquotient=dividend/divisor\nremainder=dividend%divisor\ndividend=(divisor*quotient)+remainder\nprint \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The quotient when -11 is divided by 3 is -4 = -11 div 3 and the remainder is 1 = -11 mod 3\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 246", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "#To convert binary to decimal equivalent\nbinary_num= raw_input('enter a number: ')\ndecimal = 0\nfor digit in binary_num:\n decimal = decimal*2 + int(digit)\n\nprint decimal\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter a number: 101011111\n351\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 247", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "#To convert decimal to hexadecimal\ndec= raw_input('enter a number: ')\n\nprint \"The conversion of\",dec,\"to hexadeimal is\",int(dec,16)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter a number: 2AE0B\nThe conversion of 2AE0B to hexadeimal is 175627\n"}], "metadata": {"scrolled": true, "collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 247", "cell_type": "markdown", "metadata": {}}, {"execution_count": 6, "cell_type": "code", "source": "#To compute decimal to octal\nnumbers= []\ndec=input(\"Enter a number\");\nnum=dec\nwhile dec!=0:\n \n rem=dec%8\n dec=dec/8\n numbers.append(rem)\nprint \"The decimal number\",num,\"is converted to its octal equivalent : \",\nfor i in reversed(numbers):\n print i,\n \n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter a number12345\nThe decimal number 12345 is converted to its octal equivalent : 3 0 0 7 1\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 05: Page 248", "cell_type": "markdown", "metadata": {}}, {"execution_count": 7, "cell_type": "code", "source": "#To convert Decimal to hexadecimal\nnum=[]\ndef ChangeHex(n): #function to convert\n if (n < 0):\n num.append(\"\")\n elif (n<=1):\n num.append(n)\n else: #for numbers greater than 9\n x =(n%16)\n if (x < 10):\n num.append(x) \n if (x == 10):\n num.append(\"A\")\n if (x == 11):\n num.append(\"B\")\n if (x == 12):\n num.append(\"C\")\n if (x == 13):\n num.append(\"D\")\n if (x == 14):\n num.append(\"E\")\n if (x == 15):\n num.append(\"F\")\n ChangeHex( n / 16 )\ndec_num=input(\"Enter the decimal number which is to be converted to hexadecimal\");\nChangeHex(dec_num)\nprint \"The hexadecimal equivalent of decimal\",dec_num,\"is\",\nfor i in reversed(num):\n print i,\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the decimal number which is to be converted to hexadecimal177130\nThe hexadecimal equivalent of decimal 177130 is 0 2 B 3 E A\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 06: Page 249", "cell_type": "markdown", "metadata": {}}, {"execution_count": 9, "cell_type": "code", "source": "#Compute Decimal to Binary\narray=[]\ndef conv(n):\n if n==0:\n print ''\n else:\n array.append(str(n%2)) #to compute remainder and append it to the result\n return conv(n/2) \ndec_num=input(\"Enter a decimal number\")\nconv(dec_num)\nprint \"The binary equivalent of decimal\",dec_num,\"is\",\nfor i in reversed(array):\n print i,\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter a decimal number241\n\nThe binary equivalent of decimal 241 is 1 1 1 1 0 0 0 1\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 06: Page 249", "cell_type": "markdown", "metadata": {}}, {"execution_count": 10, "cell_type": "code", "source": "#To compute the binary addition\ndef binAdd(bin1, bin2): #function to add two binary numbers\n if not bin1 or not bin2:#checks if both the numbers are binary\n return '' \n\n maxlen = max(len(bin1), len(bin2))\n\n bin1 = bin1.zfill(maxlen) #zfill fills with zero to fill the entire width\n bin2 = bin2.zfill(maxlen)\n\n result = ''\n carry = 0\n\n i = maxlen - 1\n while(i >= 0):\n s = int(bin1[i]) + int(bin2[i])#adding bit by bit\n if s == 2: #1+1\n if carry == 0:\n carry = 1\n result = \"%s%s\" % (result, '0')\n else:\n result = \"%s%s\" % (result, '1')\n elif s == 1: # 1+0\n if carry == 1:\n result = \"%s%s\" % (result, '0')\n else:\n result = \"%s%s\" % (result, '1')\n else: # 0+0\n if carry == 1:\n result = \"%s%s\" % (result, '1')\n carry = 0 \n else:\n result = \"%s%s\" % (result, '0') \n\n i = i - 1;\n\n if carry>0:\n result = \"%s%s\" % (result, '1')\n return result[::-1]\nbin1 = raw_input('enter the first number: ')\nbin2 = raw_input('enter the second number: ')\nprint \"The sum of binary numbers\",bin1,\"and\",bin2,\"is\",binAdd(bin1,bin2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter the first number: 1110\nenter the second number: 1011\nThe sum of binary numbers 1110 and 1011 is 11001\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 02: Page 258", "cell_type": "markdown", "metadata": {}}, {"execution_count": 21, "cell_type": "code", "source": "#to find the prime factors\n\ndef prime_factors(n):\n i = 2\n factors = []\n while i * i <= n:\n if n % i: #modulp division to check of the number is prime or not\n i += 1\n else:\n n //= i\n factors.append(i) #append those numbers which readily divides the given number\n if n > 1:\n factors.append(n)\n return factors\nnumber=input(\"Enter the number for which the prime factors have to be found\");\na=prime_factors(number)\nprint a\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number for which the prime factors have to be found100\n[2, 2, 5, 5]\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 258", "cell_type": "markdown", "metadata": {}}, {"execution_count": 22, "cell_type": "code", "source": "#To say if a number is prime or not\nglobals() ['count']=0\nn=input(\"Enter the number\");\nfor i in range(2,n):#number thats not divisible by other than one and itself. so from 2 to n (n-1 in python for loop)\n if n%i==0:\n count=count+1\n num=i\nif count==0:\n print n,\"is prime\" \nelse:\n print n,\"is not prime because its divisible by\",num\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number101\n101 is prime\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 259", "cell_type": "markdown", "metadata": {}}, {"execution_count": 23, "cell_type": "code", "source": "#to find the prime factors\n\ndef prime_factors(n):\n i = 2\n factors = []\n while i * i <= n:\n if n % i: #modulp division to check of the number is prime or not\n i += 1\n else:\n n //= i\n factors.append(i) #append those numbers which readily divides the given number\n if n > 1:\n factors.append(n)\n return factors\nnumber=input(\"Enter the number for which the prime factors have to be found\");\na=prime_factors(number)\nprint a\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number for which the prime factors have to be found7007\n[7, 7, 11, 13]\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 10: Page 263", "cell_type": "markdown", "metadata": {}}, {"execution_count": 24, "cell_type": "code", "source": "#To compute GCD\ndef gcd(a,b):#fuction computes gcd\n if b > a:\n return gcd(b,a)\n r = a%b\n if r == 0:\n return b\n return gcd(r,b)\nn1=input(\"Enter the first number\");\nn2=input(\"Enter the second number\");\nprint \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number24\nEnter the second number36\nGCD( 24 , 36 ) is 12\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 11: Page 263", "cell_type": "markdown", "metadata": {}}, {"execution_count": 25, "cell_type": "code", "source": "#To compute GCD\ndef gcd(a,b):#fuction computes gcd\n if b > a:\n return gcd(b,a)\n r = a%b\n if r == 0:\n return b\n return gcd(r,b)\nn1=input(\"Enter the first number\");\nn2=input(\"Enter the second number\");\nprint \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number17\nEnter the second number22\nGCD( 17 , 22 ) is 1\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 16: Page 268", "cell_type": "markdown", "metadata": {}}, {"execution_count": 26, "cell_type": "code", "source": "#to find gcd using euclidean algorithm\ndef gcd(a,b):#euclidean algithm definition\n x=a\n y=b\n while y!=0:\n r=x%y\n x=y\n y=r\n print \"gcd(\",a,\",\",b,\")is\",x\nnum1=input(\"Enter the first number\");\nnum2=input(\"Enter the second number\");\ngcd(num1,num2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number414\nEnter the second number662\ngcd( 414 , 662 )is 2\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {"collapsed": true}}, {"source": "## Example 17: Page 270", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "#to find gcd using euclidean algorithm\ndef gcd(a,b):#euclidean algithm definition\n x=a\n y=b\n while y!=0:\n r=x%y\n x=y\n y=r\n print \"gcd(\",a,\",\",b,\")is\",x\nnum1=input(\"Enter the first number\");\nnum2=input(\"Enter the second number\");\ngcd(num1,num2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number252\nEnter the second number198\ngcd( 252 , 198 )is 18\n"}], "metadata": {"collapsed": false, "trusted": true}}], "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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_3.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_3.ipynb
new file mode 100644
index 00000000..074d0a2b
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter4_3.ipynb
@@ -0,0 +1 @@
+{"nbformat_minor": 0, "cells": [{"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 239", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "#To find the quotient and remainder \ndividend=101\ndivisor=11\nquotient=dividend/divisor #To find quotient\nremainder=dividend%divisor #To find remainder\ndividend=(divisor*quotient)+remainder\nprint \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The quotient when 101 is divided by 11 is 9 = 101 div 11 and the remainder is 2 = 101 mod 11\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 240", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "#To find the quotient and remainder\ndividend=-11\ndivisor=3\nquotient=dividend/divisor\nremainder=dividend%divisor\ndividend=(divisor*quotient)+remainder\nprint \"The quotient when\",dividend,\"is divided by\",divisor,\"is\",quotient,\"=\",dividend,\"div\",divisor,\"and the remainder is\",remainder,\"=\",dividend,\"mod\",divisor\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The quotient when -11 is divided by 3 is -4 = -11 div 3 and the remainder is 1 = -11 mod 3\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 246", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "#To convert binary to decimal equivalent\nbinary_num= raw_input('enter a number: ')\ndecimal = 0\nfor digit in binary_num:\n decimal = decimal*2 + int(digit)\n\nprint decimal\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter a number: 101011111\n351\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 247", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "#To convert decimal to hexadecimal\ndec= raw_input('enter a number: ')\n\nprint \"The conversion of\",dec,\"to hexadeimal is\",int(dec,16)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter a number: 2AE0B\nThe conversion of 2AE0B to hexadeimal is 175627\n"}], "metadata": {"scrolled": true, "collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 247", "cell_type": "markdown", "metadata": {}}, {"execution_count": 6, "cell_type": "code", "source": "#To compute decimal to octal\nnumbers= []\ndec=input(\"Enter a number\");\nnum=dec\nwhile dec!=0:\n \n rem=dec%8\n dec=dec/8\n numbers.append(rem)\nprint \"The decimal number\",num,\"is converted to its octal equivalent : \",\nfor i in reversed(numbers):\n print i,\n \n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter a number12345\nThe decimal number 12345 is converted to its octal equivalent : 3 0 0 7 1\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 05: Page 248", "cell_type": "markdown", "metadata": {}}, {"execution_count": 7, "cell_type": "code", "source": "#To convert Decimal to hexadecimal\nnum=[]\ndef ChangeHex(n): #function to convert\n if (n < 0):\n num.append(\"\")\n elif (n<=1):\n num.append(n)\n else: #for numbers greater than 9\n x =(n%16)\n if (x < 10):\n num.append(x) \n if (x == 10):\n num.append(\"A\")\n if (x == 11):\n num.append(\"B\")\n if (x == 12):\n num.append(\"C\")\n if (x == 13):\n num.append(\"D\")\n if (x == 14):\n num.append(\"E\")\n if (x == 15):\n num.append(\"F\")\n ChangeHex( n / 16 )\ndec_num=input(\"Enter the decimal number which is to be converted to hexadecimal\");\nChangeHex(dec_num)\nprint \"The hexadecimal equivalent of decimal\",dec_num,\"is\",\nfor i in reversed(num):\n print i,\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the decimal number which is to be converted to hexadecimal177130\nThe hexadecimal equivalent of decimal 177130 is 0 2 B 3 E A\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 06: Page 249", "cell_type": "markdown", "metadata": {}}, {"execution_count": 9, "cell_type": "code", "source": "#Compute Decimal to Binary\narray=[]\ndef conv(n):\n if n==0:\n print ''\n else:\n array.append(str(n%2)) #to compute remainder and append it to the result\n return conv(n/2) \ndec_num=input(\"Enter a decimal number\")\nconv(dec_num)\nprint \"The binary equivalent of decimal\",dec_num,\"is\",\nfor i in reversed(array):\n print i,\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter a decimal number241\n\nThe binary equivalent of decimal 241 is 1 1 1 1 0 0 0 1\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 06: Page 249", "cell_type": "markdown", "metadata": {}}, {"execution_count": 10, "cell_type": "code", "source": "#To compute the binary addition\ndef binAdd(bin1, bin2): #function to add two binary numbers\n if not bin1 or not bin2:#checks if both the numbers are binary\n return '' \n\n maxlen = max(len(bin1), len(bin2))\n\n bin1 = bin1.zfill(maxlen) #zfill fills with zero to fill the entire width\n bin2 = bin2.zfill(maxlen)\n\n result = ''\n carry = 0\n\n i = maxlen - 1\n while(i >= 0):\n s = int(bin1[i]) + int(bin2[i])#adding bit by bit\n if s == 2: #1+1\n if carry == 0:\n carry = 1\n result = \"%s%s\" % (result, '0')\n else:\n result = \"%s%s\" % (result, '1')\n elif s == 1: # 1+0\n if carry == 1:\n result = \"%s%s\" % (result, '0')\n else:\n result = \"%s%s\" % (result, '1')\n else: # 0+0\n if carry == 1:\n result = \"%s%s\" % (result, '1')\n carry = 0 \n else:\n result = \"%s%s\" % (result, '0') \n\n i = i - 1;\n\n if carry>0:\n result = \"%s%s\" % (result, '1')\n return result[::-1]\nbin1 = raw_input('enter the first number: ')\nbin2 = raw_input('enter the second number: ')\nprint \"The sum of binary numbers\",bin1,\"and\",bin2,\"is\",binAdd(bin1,bin2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter the first number: 1110\nenter the second number: 1011\nThe sum of binary numbers 1110 and 1011 is 11001\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 02: Page 258", "cell_type": "markdown", "metadata": {}}, {"execution_count": 21, "cell_type": "code", "source": "#to find the prime factors\n\ndef prime_factors(n):\n i = 2\n factors = []\n while i * i <= n:\n if n % i: #modulp division to check of the number is prime or not\n i += 1\n else:\n n //= i\n factors.append(i) #append those numbers which readily divides the given number\n if n > 1:\n factors.append(n)\n return factors\nnumber=input(\"Enter the number for which the prime factors have to be found\");\na=prime_factors(number)\nprint a\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number for which the prime factors have to be found100\n[2, 2, 5, 5]\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 258", "cell_type": "markdown", "metadata": {}}, {"execution_count": 22, "cell_type": "code", "source": "#To say if a number is prime or not\nglobals() ['count']=0\nn=input(\"Enter the number\");\nfor i in range(2,n):#number thats not divisible by other than one and itself. so from 2 to n (n-1 in python for loop)\n if n%i==0:\n count=count+1\n num=i\nif count==0:\n print n,\"is prime\" \nelse:\n print n,\"is not prime because its divisible by\",num\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number101\n101 is prime\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 259", "cell_type": "markdown", "metadata": {}}, {"execution_count": 23, "cell_type": "code", "source": "#to find the prime factors\n\ndef prime_factors(n):\n i = 2\n factors = []\n while i * i <= n:\n if n % i: #modulp division to check of the number is prime or not\n i += 1\n else:\n n //= i\n factors.append(i) #append those numbers which readily divides the given number\n if n > 1:\n factors.append(n)\n return factors\nnumber=input(\"Enter the number for which the prime factors have to be found\");\na=prime_factors(number)\nprint a\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number for which the prime factors have to be found7007\n[7, 7, 11, 13]\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 10: Page 263", "cell_type": "markdown", "metadata": {}}, {"execution_count": 24, "cell_type": "code", "source": "#To compute GCD\ndef gcd(a,b):#fuction computes gcd\n if b > a:\n return gcd(b,a)\n r = a%b\n if r == 0:\n return b\n return gcd(r,b)\nn1=input(\"Enter the first number\");\nn2=input(\"Enter the second number\");\nprint \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number24\nEnter the second number36\nGCD( 24 , 36 ) is 12\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 11: Page 263", "cell_type": "markdown", "metadata": {}}, {"execution_count": 25, "cell_type": "code", "source": "#To compute GCD\ndef gcd(a,b):#fuction computes gcd\n if b > a:\n return gcd(b,a)\n r = a%b\n if r == 0:\n return b\n return gcd(r,b)\nn1=input(\"Enter the first number\");\nn2=input(\"Enter the second number\");\nprint \"GCD(\",n1,\",\",n2,\") is\",gcd(n1,n2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number17\nEnter the second number22\nGCD( 17 , 22 ) is 1\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 16: Page 268", "cell_type": "markdown", "metadata": {}}, {"execution_count": 26, "cell_type": "code", "source": "#to find gcd using euclidean algorithm\ndef gcd(a,b):#euclidean algithm definition\n x=a\n y=b\n while y!=0:\n r=x%y\n x=y\n y=r\n print \"gcd(\",a,\",\",b,\")is\",x\nnum1=input(\"Enter the first number\");\nnum2=input(\"Enter the second number\");\ngcd(num1,num2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number414\nEnter the second number662\ngcd( 414 , 662 )is 2\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 04: Number Theory and Cryptography", "cell_type": "markdown", "metadata": {"collapsed": true}}, {"source": "## Example 17: Page 270", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "#to find gcd using euclidean algorithm\ndef gcd(a,b):#euclidean algithm definition\n x=a\n y=b\n while y!=0:\n r=x%y\n x=y\n y=r\n print \"gcd(\",a,\",\",b,\")is\",x\nnum1=input(\"Enter the first number\");\nnum2=input(\"Enter the second number\");\ngcd(num1,num2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number252\nEnter the second number198\ngcd( 252 , 198 )is 18\n"}], "metadata": {"collapsed": false, "trusted": true}}], "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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5.ipynb
new file mode 100755
index 00000000..14960493
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5.ipynb
@@ -0,0 +1,312 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 346"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The value of f( 1 ) is 9\n",
+ "The value of f( 2 ) is 21\n",
+ "The value of f( 3 ) is 45\n",
+ "The value of f( 4 ) is 93\n"
+ ]
+ }
+ ],
+ "source": [
+ "#to compute the recursive functions\n",
+ "def f(n):\n",
+ "\n",
+ " if n==0:\n",
+ " return 3\n",
+ " else:\n",
+ " n=n-1\n",
+ " result=2*f(n)+3 #recursive call\n",
+ " return result\n",
+ "for num in range(1,5):\n",
+ " r=f(num)\n",
+ " print \"The value of f(\",num,\") is\",r #Prints the result for individual instance\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter a number whose factorial is to be found4\n",
+ "The factorial of 4 is 24\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute the factorial of a given number using recursion\n",
+ "def factorial(n):\n",
+ " if n==0:\n",
+ " return 1\n",
+ " else:\n",
+ " return n*factorial(n-1) #recursive function call\n",
+ "num=input(\"Enter a number whose factorial is to be found\");\n",
+ "print \"The factorial of\",num,\"is\",factorial(num);\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 02: Page 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number5\n",
+ "Enter the power4\n",
+ "The value of 5 to the power 4 is 625\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute power using recursive algorithm\n",
+ "def power(a,n):\n",
+ " if n==0:\n",
+ " return 1\n",
+ " else:\n",
+ " return a*power(a,n-1) #recursive call algorithm\n",
+ "num=input(\"Enter the number\");\n",
+ "p=input(\"Enter the power\");\n",
+ "print \"The value of\",num,\"to the power\",p,\"is\",power(num,p);\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 362"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the first number5\n",
+ "Enter the second number8\n",
+ "The gcd of 5 , 8 is 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute gcd using modular recursion\n",
+ "def gcd(a,b):\n",
+ " if a==0:\n",
+ " return b\n",
+ " else:\n",
+ " return gcd(b%a,a) #recursive call\n",
+ "\n",
+ "num1=input(\"Enter the first number\")\n",
+ "num2=input(\"Enter the second number\")\n",
+ "print \"The gcd of\",num1,\",\",num2,\"is\",gcd(num1,num2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 362"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number2\n",
+ "Enter the power5\n",
+ "Enter the modulo number3\n",
+ "The answer of mpower( 2 , 5 , 3 ) is 2\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute mpower function using recursion\n",
+ "def mpower(b,n,m):\n",
+ " if n==0:\n",
+ " return 1\n",
+ " else:\n",
+ " if n%2==0:\n",
+ " return ((mpower(b,n/2,m))**2) % m #recursive call\n",
+ " else:\n",
+ " return ((mpower(b,n/2,m)**2)%m*(b%m))%m #recursive call\n",
+ "number=input(\"Enter the number\")\n",
+ "power=input(\"Enter the power\")\n",
+ "modulo=input(\"Enter the modulo number\");\n",
+ "print \"The answer of mpower(\",number,\",\",power,\",\",modulo,\") is\",mpower(number,power,modulo)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 09: Page 367"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "enter the numbers to be merge sorted8,2,4,6,9,7,10,1,5,3\n",
+ "Ther Merge sort is ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']\n"
+ ]
+ }
+ ],
+ "source": [
+ "def msort2(x): #function for merge sort\n",
+ " if len(x) < 2:\n",
+ " return x\n",
+ " result = [] \n",
+ " mid = int(len(x)/2) #divides the elements into halves\n",
+ " y = msort2(x[:mid])\n",
+ " z = msort2(x[mid:])\n",
+ " while (len(y) > 0) and (len(z) > 0):\n",
+ " if y[0] > z[0]:\n",
+ " result.append(z[0]) #merges to append the elements\n",
+ " z.pop(0)\n",
+ " else:\n",
+ " result.append(y[0])\n",
+ " y.pop(0)\n",
+ " result += y\n",
+ " result += z\n",
+ " return result\n",
+ "l=[]\n",
+ "r=[]\n",
+ "l=raw_input(\"enter the numbers to be merge sorted\").split(\",\")\n",
+ "r=msort2(l)\n",
+ "print \"Ther Merge sort is\", r\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_1.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_1.ipynb
new file mode 100755
index 00000000..14960493
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_1.ipynb
@@ -0,0 +1,312 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 346"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The value of f( 1 ) is 9\n",
+ "The value of f( 2 ) is 21\n",
+ "The value of f( 3 ) is 45\n",
+ "The value of f( 4 ) is 93\n"
+ ]
+ }
+ ],
+ "source": [
+ "#to compute the recursive functions\n",
+ "def f(n):\n",
+ "\n",
+ " if n==0:\n",
+ " return 3\n",
+ " else:\n",
+ " n=n-1\n",
+ " result=2*f(n)+3 #recursive call\n",
+ " return result\n",
+ "for num in range(1,5):\n",
+ " r=f(num)\n",
+ " print \"The value of f(\",num,\") is\",r #Prints the result for individual instance\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter a number whose factorial is to be found4\n",
+ "The factorial of 4 is 24\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute the factorial of a given number using recursion\n",
+ "def factorial(n):\n",
+ " if n==0:\n",
+ " return 1\n",
+ " else:\n",
+ " return n*factorial(n-1) #recursive function call\n",
+ "num=input(\"Enter a number whose factorial is to be found\");\n",
+ "print \"The factorial of\",num,\"is\",factorial(num);\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 02: Page 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number5\n",
+ "Enter the power4\n",
+ "The value of 5 to the power 4 is 625\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute power using recursive algorithm\n",
+ "def power(a,n):\n",
+ " if n==0:\n",
+ " return 1\n",
+ " else:\n",
+ " return a*power(a,n-1) #recursive call algorithm\n",
+ "num=input(\"Enter the number\");\n",
+ "p=input(\"Enter the power\");\n",
+ "print \"The value of\",num,\"to the power\",p,\"is\",power(num,p);\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 362"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the first number5\n",
+ "Enter the second number8\n",
+ "The gcd of 5 , 8 is 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute gcd using modular recursion\n",
+ "def gcd(a,b):\n",
+ " if a==0:\n",
+ " return b\n",
+ " else:\n",
+ " return gcd(b%a,a) #recursive call\n",
+ "\n",
+ "num1=input(\"Enter the first number\")\n",
+ "num2=input(\"Enter the second number\")\n",
+ "print \"The gcd of\",num1,\",\",num2,\"is\",gcd(num1,num2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 362"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number2\n",
+ "Enter the power5\n",
+ "Enter the modulo number3\n",
+ "The answer of mpower( 2 , 5 , 3 ) is 2\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute mpower function using recursion\n",
+ "def mpower(b,n,m):\n",
+ " if n==0:\n",
+ " return 1\n",
+ " else:\n",
+ " if n%2==0:\n",
+ " return ((mpower(b,n/2,m))**2) % m #recursive call\n",
+ " else:\n",
+ " return ((mpower(b,n/2,m)**2)%m*(b%m))%m #recursive call\n",
+ "number=input(\"Enter the number\")\n",
+ "power=input(\"Enter the power\")\n",
+ "modulo=input(\"Enter the modulo number\");\n",
+ "print \"The answer of mpower(\",number,\",\",power,\",\",modulo,\") is\",mpower(number,power,modulo)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 09: Page 367"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "enter the numbers to be merge sorted8,2,4,6,9,7,10,1,5,3\n",
+ "Ther Merge sort is ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']\n"
+ ]
+ }
+ ],
+ "source": [
+ "def msort2(x): #function for merge sort\n",
+ " if len(x) < 2:\n",
+ " return x\n",
+ " result = [] \n",
+ " mid = int(len(x)/2) #divides the elements into halves\n",
+ " y = msort2(x[:mid])\n",
+ " z = msort2(x[mid:])\n",
+ " while (len(y) > 0) and (len(z) > 0):\n",
+ " if y[0] > z[0]:\n",
+ " result.append(z[0]) #merges to append the elements\n",
+ " z.pop(0)\n",
+ " else:\n",
+ " result.append(y[0])\n",
+ " y.pop(0)\n",
+ " result += y\n",
+ " result += z\n",
+ " return result\n",
+ "l=[]\n",
+ "r=[]\n",
+ "l=raw_input(\"enter the numbers to be merge sorted\").split(\",\")\n",
+ "r=msort2(l)\n",
+ "print \"Ther Merge sort is\", r\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_2.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_2.ipynb
new file mode 100644
index 00000000..dd13690b
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_2.ipynb
@@ -0,0 +1 @@
+{"nbformat_minor": 0, "cells": [{"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 346", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "#to compute the recursive functions\ndef f(n):\n\n if n==0:\n return 3\n else:\n n=n-1\n result=2*f(n)+3 #recursive call\n return result\nfor num in range(1,5):\n r=f(num)\n print \"The value of f(\",num,\") is\",r #Prints the result for individual instance\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The value of f( 1 ) is 9\nThe value of f( 2 ) is 21\nThe value of f( 3 ) is 45\nThe value of f( 4 ) is 93\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 361", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "#To compute the factorial of a given number using recursion\ndef factorial(n):\n if n==0:\n return 1\n else:\n return n*factorial(n-1) #recursive function call\nnum=input(\"Enter a number whose factorial is to be found\");\nprint \"The factorial of\",num,\"is\",factorial(num);\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter a number whose factorial is to be found4\nThe factorial of 4 is 24\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 02: Page 361", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "#To compute power using recursive algorithm\ndef power(a,n):\n if n==0:\n return 1\n else:\n return a*power(a,n-1) #recursive call algorithm\nnum=input(\"Enter the number\");\np=input(\"Enter the power\");\nprint \"The value of\",num,\"to the power\",p,\"is\",power(num,p);\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number5\nEnter the power4\nThe value of 5 to the power 4 is 625\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 362", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "#To compute gcd using modular recursion\ndef gcd(a,b):\n if a==0:\n return b\n else:\n return gcd(b%a,a) #recursive call\n\nnum1=input(\"Enter the first number\")\nnum2=input(\"Enter the second number\")\nprint \"The gcd of\",num1,\",\",num2,\"is\",gcd(num1,num2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number5\nEnter the second number8\nThe gcd of 5 , 8 is 1\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 362", "cell_type": "markdown", "metadata": {}}, {"execution_count": 4, "cell_type": "code", "source": "#To compute mpower function using recursion\ndef mpower(b,n,m):\n if n==0:\n return 1\n else:\n if n%2==0:\n return ((mpower(b,n/2,m))**2) % m #recursive call\n else:\n return ((mpower(b,n/2,m)**2)%m*(b%m))%m #recursive call\nnumber=input(\"Enter the number\")\npower=input(\"Enter the power\")\nmodulo=input(\"Enter the modulo number\");\nprint \"The answer of mpower(\",number,\",\",power,\",\",modulo,\") is\",mpower(number,power,modulo)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number2\nEnter the power5\nEnter the modulo number3\nThe answer of mpower( 2 , 5 , 3 ) is 2\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 09: Page 367", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "def msort2(x): #function for merge sort\n if len(x) < 2:\n return x\n result = [] \n mid = int(len(x)/2) #divides the elements into halves\n y = msort2(x[:mid])\n z = msort2(x[mid:])\n while (len(y) > 0) and (len(z) > 0):\n if y[0] > z[0]:\n result.append(z[0]) #merges to append the elements\n z.pop(0)\n else:\n result.append(y[0])\n y.pop(0)\n result += y\n result += z\n return result\nl=[]\nr=[]\nl=raw_input(\"enter the numbers to be merge sorted\").split(\",\")\nr=msort2(l)\nprint \"Ther Merge sort is\", r\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter the numbers to be merge sorted8,2,4,6,9,7,10,1,5,3\nTher Merge sort is ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']\n"}], "metadata": {"collapsed": false, "trusted": true}}], "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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_3.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_3.ipynb
new file mode 100644
index 00000000..dd13690b
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter5_3.ipynb
@@ -0,0 +1 @@
+{"nbformat_minor": 0, "cells": [{"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 346", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "#to compute the recursive functions\ndef f(n):\n\n if n==0:\n return 3\n else:\n n=n-1\n result=2*f(n)+3 #recursive call\n return result\nfor num in range(1,5):\n r=f(num)\n print \"The value of f(\",num,\") is\",r #Prints the result for individual instance\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The value of f( 1 ) is 9\nThe value of f( 2 ) is 21\nThe value of f( 3 ) is 45\nThe value of f( 4 ) is 93\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 361", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "#To compute the factorial of a given number using recursion\ndef factorial(n):\n if n==0:\n return 1\n else:\n return n*factorial(n-1) #recursive function call\nnum=input(\"Enter a number whose factorial is to be found\");\nprint \"The factorial of\",num,\"is\",factorial(num);\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter a number whose factorial is to be found4\nThe factorial of 4 is 24\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 02: Page 361", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "#To compute power using recursive algorithm\ndef power(a,n):\n if n==0:\n return 1\n else:\n return a*power(a,n-1) #recursive call algorithm\nnum=input(\"Enter the number\");\np=input(\"Enter the power\");\nprint \"The value of\",num,\"to the power\",p,\"is\",power(num,p);\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number5\nEnter the power4\nThe value of 5 to the power 4 is 625\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 362", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "#To compute gcd using modular recursion\ndef gcd(a,b):\n if a==0:\n return b\n else:\n return gcd(b%a,a) #recursive call\n\nnum1=input(\"Enter the first number\")\nnum2=input(\"Enter the second number\")\nprint \"The gcd of\",num1,\",\",num2,\"is\",gcd(num1,num2)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the first number5\nEnter the second number8\nThe gcd of 5 , 8 is 1\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 362", "cell_type": "markdown", "metadata": {}}, {"execution_count": 4, "cell_type": "code", "source": "#To compute mpower function using recursion\ndef mpower(b,n,m):\n if n==0:\n return 1\n else:\n if n%2==0:\n return ((mpower(b,n/2,m))**2) % m #recursive call\n else:\n return ((mpower(b,n/2,m)**2)%m*(b%m))%m #recursive call\nnumber=input(\"Enter the number\")\npower=input(\"Enter the power\")\nmodulo=input(\"Enter the modulo number\");\nprint \"The answer of mpower(\",number,\",\",power,\",\",modulo,\") is\",mpower(number,power,modulo)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number2\nEnter the power5\nEnter the modulo number3\nThe answer of mpower( 2 , 5 , 3 ) is 2\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 05: Induction and Recursion", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 09: Page 367", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "def msort2(x): #function for merge sort\n if len(x) < 2:\n return x\n result = [] \n mid = int(len(x)/2) #divides the elements into halves\n y = msort2(x[:mid])\n z = msort2(x[mid:])\n while (len(y) > 0) and (len(z) > 0):\n if y[0] > z[0]:\n result.append(z[0]) #merges to append the elements\n z.pop(0)\n else:\n result.append(y[0])\n y.pop(0)\n result += y\n result += z\n return result\nl=[]\nr=[]\nl=raw_input(\"enter the numbers to be merge sorted\").split(\",\")\nr=msort2(l)\nprint \"Ther Merge sort is\", r\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "enter the numbers to be merge sorted8,2,4,6,9,7,10,1,5,3\nTher Merge sort is ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']\n"}], "metadata": {"collapsed": false, "trusted": true}}], "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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6.ipynb
new file mode 100644
index 00000000..485e6b32
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6.ipynb
@@ -0,0 +1 @@
+{"nbformat_minor": 0, "cells": [{"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "n=2 #number of employees\nr=12 #number of office rooms\nways_alloc_sanchez=12\nways_alloc_patel=11\n\n#By PRODUCT RULE\nprint \"Total ways to assign offices to these employees is\",ways_alloc_sanchez*ways_alloc_patel\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Total ways to assign offices to these employees is 132\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 02: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "alphabets=26 #Total number of alphabets\nposint=100 #Total positive numbers not beyond 100\n\n#number of chairs to be labelled with a alphabet and an integer using product rule\nprint \"Total number of chairs that can be labelled with an alphabet and an integer is\",alphabets*posint\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Total number of chairs that can be labelled with an alphabet and an integer is 2600\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "mc=32 #total number of microcomputers\nport=24 #total number of ports in each microcomputer\n\n#total number of different ports to a microcomputer in the center are found using product rule\n\nprint \"total number of ports\",mc*port\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "total number of ports 768\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 4, "cell_type": "code", "source": "bits=2 #possible bits either 0 or 1\nns=7 #number of bits in the string (ie). length of the string\n # 7 bits are capable of taking either 0 or 1 so by PRODUCT RULE\nprint \"Total different bit strings of lenth seven are\",bits**ns\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Total different bit strings of lenth seven are 128\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 05: Page 387", "cell_type": "markdown", "metadata": {}}, {"execution_count": 7, "cell_type": "code", "source": "letters=26 #number of letters in english alphabet\nno_of_letters=3 #number of letters \nchoices=10 #number of choices for each letter\nresult=1#in order to avoid junk values. Assigned it to 1.\nfor i in range(0,no_of_letters):\n result=result*letters*choices\nprint \"The total number of choices are\",result", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The total number of choices are 17576000\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 407", "cell_type": "markdown", "metadata": {}}, {"execution_count": 10, "cell_type": "code", "source": "def permutation(n,r): #function definition\n \n i=n\n result=1\n for i in range((n-r)+1,n+1): #computing the permutation\n result=result*i\n \n return result\n\nprint \"The number of ways to select 3 students from a group of 5 students to line up for a picture is \",permutation(5,3) #function call\nprint \"The number of ways to select 5 students from a group of 5 students to line up for a picture is \",permutation(5,5) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The number of ways to select 3 students from a group of 5 students to line up for a picture is 60\nThe number of ways to select 5 students from a group of 5 students to line up for a picture is 120\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 409", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "def permutation(n,r): #function definition\n \n i=n\n result=1\n for i in range((n-r)+1,n+1): #permutation computation\n result=result*i\n return result\nnum=input(\"Enter the number of people\")\nperm=input(\"Enter the prizes\")\nprint \"The number of ways to decide the prize winners is\",permutation(num,perm) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of people100\nEnter the prizes3\nThe number of ways to decide the prize winners is 970200\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 05: Page 409", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "def permutation(n,r):\n \n i=n\n result=1\n for i in range((n-r)+1,n+1):\n result=result*i\n \n return result\nnum=input(\"Enter the number of runners\")\nperm=input(\"Enter the number of prizes\")\nprint \"The number of ways to decide the prize winners is\",permutation(num,perm)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of runners8\nEnter the number of prizes3\nThe number of ways to decide the prize winners is 336\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 06: Page 409", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "def calc(n):\n \n i=n\n result=1\n for i in range(1,n): #find the number of ways to decide the path. since the first city us decided. The for loop is from 1 to n\n result=result*i\n \n return result\nnum=input(\"Enter the number of cities\") \nprint \"The number of possible ways to decide the path is\",calc(num)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of cities8\nThe number of possible ways to decide the path is 5040\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 10: Page 410", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "def combination(n,r): #combination function\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1):#computes the value of the numerator \n numerator=numerator*i\n for j in range (1,r+1): #computes the value of the denominator\n denominator=denominator*j\n result=numerator/denominator #computes result\n return result\nnum=input(\"Enter the number of elements\")\ncomb=input(\"Enter the combinations\")\nprint \"The number of combinations are \",combination(num,comb)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of elements4\nEnter the combinations2\nThe number of combinations are 6\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 12: Page 412", "cell_type": "markdown", "metadata": {}}, {"execution_count": 7, "cell_type": "code", "source": "def combination(n,r): #function definition for combination\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1):\n numerator=numerator*i\n for j in range (1,r+1):\n denominator=denominator*j\n result=numerator/denominator\n return result\nnum=input(\"Enter the number of members in a team\")\ncomb=input(\"Enter the number of players\")\nprint \"The number of combinations are \",combination(num,comb) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of members in a team10\nEnter the number of players5\nThe number of combinations are 252\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 13: Page 412", "cell_type": "markdown", "metadata": {}}, {"execution_count": 8, "cell_type": "code", "source": "def combination(n,r): #function definition\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1):\n numerator=numerator*i\n for j in range (1,r+1):\n denominator=denominator*j\n result=numerator/denominator\n return result\nnum=input(\"Enter the total number of astronauts\")\ncomb=input(\"Enter the number of astronauts to be selected \")\nprint \"The total number of combinations of selected astronauts to Mars are \",combination(num,comb) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the total number of astronauts30\nEnter the number of astronauts to be selected 6\nThe total number of combinations of selected astronauts to Mars are 593775\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 15: Page 413", "cell_type": "markdown", "metadata": {}}, {"execution_count": 9, "cell_type": "code", "source": "def combination(n,r): #Function definition\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1): #computation of the numerator\n numerator=numerator*i\n for j in range (1,r+1): #computation of the denominator\n denominator=denominator*j\n result=numerator/denominator\n return result\nnum1=input(\"Enter the total number of faculty in computer science department\")\ncomb1=input(\"Enter the number of faculty to be selected for computer science department\")\nnum2=input(\"Enter the total number of faculty in maths department\")\ncomb2=input(\"Enter the number of faculty to be selected for maths department\")\n\nprint \"The total number of combinations of selected faculties are \",combination(num1,comb1)*combination(num2,comb2) #Function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the total number of faculty in computer science department9\nEnter the number of faculty to be selected for computer science department3\nEnter the total number of faculty in maths department11\nEnter the number of faculty to be selected for maths department4\nThe total number of combinations of selected faculties are 27720\n"}], "metadata": {"collapsed": false, "trusted": true}}], "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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_(1).ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_(1).ipynb
new file mode 100755
index 00000000..16677df3
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_(1).ipynb
@@ -0,0 +1,594 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 386"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Total ways to assign offices to these employees is 132\n"
+ ]
+ }
+ ],
+ "source": [
+ "n=2 #number of employees\n",
+ "r=12 #number of office rooms\n",
+ "ways_alloc_sanchez=12\n",
+ "ways_alloc_patel=11\n",
+ "\n",
+ "#By PRODUCT RULE\n",
+ "print \"Total ways to assign offices to these employees is\",ways_alloc_sanchez*ways_alloc_patel\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 02: Page 386"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Total number of chairs that can be labelled with an alphabet and an integer is 2600\n"
+ ]
+ }
+ ],
+ "source": [
+ "alphabets=26 #Total number of alphabets\n",
+ "posint=100 #Total positive numbers not beyond 100\n",
+ "\n",
+ "#number of chairs to be labelled with a alphabet and an integer using product rule\n",
+ "print \"Total number of chairs that can be labelled with an alphabet and an integer is\",alphabets*posint\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 386"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "total number of ports 768\n"
+ ]
+ }
+ ],
+ "source": [
+ "mc=32 #total number of microcomputers\n",
+ "port=24 #total number of ports in each microcomputer\n",
+ "\n",
+ "#total number of different ports to a microcomputer in the center are found using product rule\n",
+ "\n",
+ "print \"total number of ports\",mc*port\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06 Counting"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 386"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Total different bit strings of lenth seven are 128\n"
+ ]
+ }
+ ],
+ "source": [
+ "bits=2 #possible bits either 0 or 1\n",
+ "ns=7 #number of bits in the string (ie). length of the string\n",
+ " # 7 bits are capable of taking either 0 or 1 so by PRODUCT RULE\n",
+ "print \"Total different bit strings of lenth seven are\",bits**ns\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 05: Page 387"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The total number of choices are 17576000\n"
+ ]
+ }
+ ],
+ "source": [
+ "letters=26 #number of letters in english alphabet\n",
+ "no_of_letters=3 #number of letters \n",
+ "choices=10 #number of choices for each letter\n",
+ "result=1#in order to avoid junk values. Assigned it to 1.\n",
+ "for i in range(0,no_of_letters):\n",
+ " result=result*letters*choices\n",
+ "print \"The total number of choices are\",result"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 407"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The number of ways to select 3 students from a group of 5 students to line up for a picture is 60\n",
+ "The number of ways to select 5 students from a group of 5 students to line up for a picture is 120\n"
+ ]
+ }
+ ],
+ "source": [
+ "def permutation(n,r): #function definition\n",
+ " \n",
+ " i=n\n",
+ " result=1\n",
+ " for i in range((n-r)+1,n+1): #computing the permutation\n",
+ " result=result*i\n",
+ " \n",
+ " return result\n",
+ "\n",
+ "print \"The number of ways to select 3 students from a group of 5 students to line up for a picture is \",permutation(5,3) #function call\n",
+ "print \"The number of ways to select 5 students from a group of 5 students to line up for a picture is \",permutation(5,5) #function call\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 409"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of people100\n",
+ "Enter the prizes3\n",
+ "The number of ways to decide the prize winners is 970200\n"
+ ]
+ }
+ ],
+ "source": [
+ "def permutation(n,r): #function definition\n",
+ " \n",
+ " i=n\n",
+ " result=1\n",
+ " for i in range((n-r)+1,n+1): #permutation computation\n",
+ " result=result*i\n",
+ " return result\n",
+ "num=input(\"Enter the number of people\")\n",
+ "perm=input(\"Enter the prizes\")\n",
+ "print \"The number of ways to decide the prize winners is\",permutation(num,perm) #function call\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 05: Page 409"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of runners8\n",
+ "Enter the number of prizes3\n",
+ "The number of ways to decide the prize winners is 336\n"
+ ]
+ }
+ ],
+ "source": [
+ "def permutation(n,r):\n",
+ " \n",
+ " i=n\n",
+ " result=1\n",
+ " for i in range((n-r)+1,n+1):\n",
+ " result=result*i\n",
+ " \n",
+ " return result\n",
+ "num=input(\"Enter the number of runners\")\n",
+ "perm=input(\"Enter the number of prizes\")\n",
+ "print \"The number of ways to decide the prize winners is\",permutation(num,perm)\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 06: Page 409"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of cities8\n",
+ "The number of possible ways to decide the path is 5040\n"
+ ]
+ }
+ ],
+ "source": [
+ "def calc(n):\n",
+ " \n",
+ " i=n\n",
+ " result=1\n",
+ " for i in range(1,n): #find the number of ways to decide the path. since the first city us decided. The for loop is from 1 to n\n",
+ " result=result*i\n",
+ " \n",
+ " return result\n",
+ "num=input(\"Enter the number of cities\") \n",
+ "print \"The number of possible ways to decide the path is\",calc(num)\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 10: Page 410"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of elements4\n",
+ "Enter the combinations2\n",
+ "The number of combinations are 6\n"
+ ]
+ }
+ ],
+ "source": [
+ "def combination(n,r): #combination function\n",
+ " i=n\n",
+ " numerator=1\n",
+ " denominator=1\n",
+ " for i in range((n-r)+1,n+1):#computes the value of the numerator \n",
+ " numerator=numerator*i\n",
+ " for j in range (1,r+1): #computes the value of the denominator\n",
+ " denominator=denominator*j\n",
+ " result=numerator/denominator #computes result\n",
+ " return result\n",
+ "num=input(\"Enter the number of elements\")\n",
+ "comb=input(\"Enter the combinations\")\n",
+ "print \"The number of combinations are \",combination(num,comb)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 12: Page 412"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of members in a team10\n",
+ "Enter the number of players5\n",
+ "The number of combinations are 252\n"
+ ]
+ }
+ ],
+ "source": [
+ "def combination(n,r): #function definition for combination\n",
+ " i=n\n",
+ " numerator=1\n",
+ " denominator=1\n",
+ " for i in range((n-r)+1,n+1):\n",
+ " numerator=numerator*i\n",
+ " for j in range (1,r+1):\n",
+ " denominator=denominator*j\n",
+ " result=numerator/denominator\n",
+ " return result\n",
+ "num=input(\"Enter the number of members in a team\")\n",
+ "comb=input(\"Enter the number of players\")\n",
+ "print \"The number of combinations are \",combination(num,comb) #function call\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 13: Page 412"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the total number of astronauts30\n",
+ "Enter the number of astronauts to be selected 6\n",
+ "The total number of combinations of selected astronauts to Mars are 593775\n"
+ ]
+ }
+ ],
+ "source": [
+ "def combination(n,r): #function definition\n",
+ " i=n\n",
+ " numerator=1\n",
+ " denominator=1\n",
+ " for i in range((n-r)+1,n+1):\n",
+ " numerator=numerator*i\n",
+ " for j in range (1,r+1):\n",
+ " denominator=denominator*j\n",
+ " result=numerator/denominator\n",
+ " return result\n",
+ "num=input(\"Enter the total number of astronauts\")\n",
+ "comb=input(\"Enter the number of astronauts to be selected \")\n",
+ "print \"The total number of combinations of selected astronauts to Mars are \",combination(num,comb) #function call\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 15: Page 413"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the total number of faculty in computer science department9\n",
+ "Enter the number of faculty to be selected for computer science department3\n",
+ "Enter the total number of faculty in maths department11\n",
+ "Enter the number of faculty to be selected for maths department4\n",
+ "The total number of combinations of selected faculties are 27720\n"
+ ]
+ }
+ ],
+ "source": [
+ "def combination(n,r): #Function definition\n",
+ " i=n\n",
+ " numerator=1\n",
+ " denominator=1\n",
+ " for i in range((n-r)+1,n+1): #computation of the numerator\n",
+ " numerator=numerator*i\n",
+ " for j in range (1,r+1): #computation of the denominator\n",
+ " denominator=denominator*j\n",
+ " result=numerator/denominator\n",
+ " return result\n",
+ "num1=input(\"Enter the total number of faculty in computer science department\")\n",
+ "comb1=input(\"Enter the number of faculty to be selected for computer science department\")\n",
+ "num2=input(\"Enter the total number of faculty in maths department\")\n",
+ "comb2=input(\"Enter the number of faculty to be selected for maths department\")\n",
+ "\n",
+ "print \"The total number of combinations of selected faculties are \",combination(num1,comb1)*combination(num2,comb2) #Function call\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_(1)_1.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_(1)_1.ipynb
new file mode 100755
index 00000000..16677df3
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_(1)_1.ipynb
@@ -0,0 +1,594 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 386"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Total ways to assign offices to these employees is 132\n"
+ ]
+ }
+ ],
+ "source": [
+ "n=2 #number of employees\n",
+ "r=12 #number of office rooms\n",
+ "ways_alloc_sanchez=12\n",
+ "ways_alloc_patel=11\n",
+ "\n",
+ "#By PRODUCT RULE\n",
+ "print \"Total ways to assign offices to these employees is\",ways_alloc_sanchez*ways_alloc_patel\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 02: Page 386"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Total number of chairs that can be labelled with an alphabet and an integer is 2600\n"
+ ]
+ }
+ ],
+ "source": [
+ "alphabets=26 #Total number of alphabets\n",
+ "posint=100 #Total positive numbers not beyond 100\n",
+ "\n",
+ "#number of chairs to be labelled with a alphabet and an integer using product rule\n",
+ "print \"Total number of chairs that can be labelled with an alphabet and an integer is\",alphabets*posint\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 386"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "total number of ports 768\n"
+ ]
+ }
+ ],
+ "source": [
+ "mc=32 #total number of microcomputers\n",
+ "port=24 #total number of ports in each microcomputer\n",
+ "\n",
+ "#total number of different ports to a microcomputer in the center are found using product rule\n",
+ "\n",
+ "print \"total number of ports\",mc*port\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06 Counting"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 386"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Total different bit strings of lenth seven are 128\n"
+ ]
+ }
+ ],
+ "source": [
+ "bits=2 #possible bits either 0 or 1\n",
+ "ns=7 #number of bits in the string (ie). length of the string\n",
+ " # 7 bits are capable of taking either 0 or 1 so by PRODUCT RULE\n",
+ "print \"Total different bit strings of lenth seven are\",bits**ns\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 05: Page 387"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The total number of choices are 17576000\n"
+ ]
+ }
+ ],
+ "source": [
+ "letters=26 #number of letters in english alphabet\n",
+ "no_of_letters=3 #number of letters \n",
+ "choices=10 #number of choices for each letter\n",
+ "result=1#in order to avoid junk values. Assigned it to 1.\n",
+ "for i in range(0,no_of_letters):\n",
+ " result=result*letters*choices\n",
+ "print \"The total number of choices are\",result"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 407"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The number of ways to select 3 students from a group of 5 students to line up for a picture is 60\n",
+ "The number of ways to select 5 students from a group of 5 students to line up for a picture is 120\n"
+ ]
+ }
+ ],
+ "source": [
+ "def permutation(n,r): #function definition\n",
+ " \n",
+ " i=n\n",
+ " result=1\n",
+ " for i in range((n-r)+1,n+1): #computing the permutation\n",
+ " result=result*i\n",
+ " \n",
+ " return result\n",
+ "\n",
+ "print \"The number of ways to select 3 students from a group of 5 students to line up for a picture is \",permutation(5,3) #function call\n",
+ "print \"The number of ways to select 5 students from a group of 5 students to line up for a picture is \",permutation(5,5) #function call\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 409"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of people100\n",
+ "Enter the prizes3\n",
+ "The number of ways to decide the prize winners is 970200\n"
+ ]
+ }
+ ],
+ "source": [
+ "def permutation(n,r): #function definition\n",
+ " \n",
+ " i=n\n",
+ " result=1\n",
+ " for i in range((n-r)+1,n+1): #permutation computation\n",
+ " result=result*i\n",
+ " return result\n",
+ "num=input(\"Enter the number of people\")\n",
+ "perm=input(\"Enter the prizes\")\n",
+ "print \"The number of ways to decide the prize winners is\",permutation(num,perm) #function call\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 05: Page 409"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of runners8\n",
+ "Enter the number of prizes3\n",
+ "The number of ways to decide the prize winners is 336\n"
+ ]
+ }
+ ],
+ "source": [
+ "def permutation(n,r):\n",
+ " \n",
+ " i=n\n",
+ " result=1\n",
+ " for i in range((n-r)+1,n+1):\n",
+ " result=result*i\n",
+ " \n",
+ " return result\n",
+ "num=input(\"Enter the number of runners\")\n",
+ "perm=input(\"Enter the number of prizes\")\n",
+ "print \"The number of ways to decide the prize winners is\",permutation(num,perm)\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 06: Page 409"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of cities8\n",
+ "The number of possible ways to decide the path is 5040\n"
+ ]
+ }
+ ],
+ "source": [
+ "def calc(n):\n",
+ " \n",
+ " i=n\n",
+ " result=1\n",
+ " for i in range(1,n): #find the number of ways to decide the path. since the first city us decided. The for loop is from 1 to n\n",
+ " result=result*i\n",
+ " \n",
+ " return result\n",
+ "num=input(\"Enter the number of cities\") \n",
+ "print \"The number of possible ways to decide the path is\",calc(num)\n",
+ "\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 10: Page 410"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of elements4\n",
+ "Enter the combinations2\n",
+ "The number of combinations are 6\n"
+ ]
+ }
+ ],
+ "source": [
+ "def combination(n,r): #combination function\n",
+ " i=n\n",
+ " numerator=1\n",
+ " denominator=1\n",
+ " for i in range((n-r)+1,n+1):#computes the value of the numerator \n",
+ " numerator=numerator*i\n",
+ " for j in range (1,r+1): #computes the value of the denominator\n",
+ " denominator=denominator*j\n",
+ " result=numerator/denominator #computes result\n",
+ " return result\n",
+ "num=input(\"Enter the number of elements\")\n",
+ "comb=input(\"Enter the combinations\")\n",
+ "print \"The number of combinations are \",combination(num,comb)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 12: Page 412"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number of members in a team10\n",
+ "Enter the number of players5\n",
+ "The number of combinations are 252\n"
+ ]
+ }
+ ],
+ "source": [
+ "def combination(n,r): #function definition for combination\n",
+ " i=n\n",
+ " numerator=1\n",
+ " denominator=1\n",
+ " for i in range((n-r)+1,n+1):\n",
+ " numerator=numerator*i\n",
+ " for j in range (1,r+1):\n",
+ " denominator=denominator*j\n",
+ " result=numerator/denominator\n",
+ " return result\n",
+ "num=input(\"Enter the number of members in a team\")\n",
+ "comb=input(\"Enter the number of players\")\n",
+ "print \"The number of combinations are \",combination(num,comb) #function call\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 13: Page 412"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the total number of astronauts30\n",
+ "Enter the number of astronauts to be selected 6\n",
+ "The total number of combinations of selected astronauts to Mars are 593775\n"
+ ]
+ }
+ ],
+ "source": [
+ "def combination(n,r): #function definition\n",
+ " i=n\n",
+ " numerator=1\n",
+ " denominator=1\n",
+ " for i in range((n-r)+1,n+1):\n",
+ " numerator=numerator*i\n",
+ " for j in range (1,r+1):\n",
+ " denominator=denominator*j\n",
+ " result=numerator/denominator\n",
+ " return result\n",
+ "num=input(\"Enter the total number of astronauts\")\n",
+ "comb=input(\"Enter the number of astronauts to be selected \")\n",
+ "print \"The total number of combinations of selected astronauts to Mars are \",combination(num,comb) #function call\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 06:COUNTING"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 15: Page 413"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the total number of faculty in computer science department9\n",
+ "Enter the number of faculty to be selected for computer science department3\n",
+ "Enter the total number of faculty in maths department11\n",
+ "Enter the number of faculty to be selected for maths department4\n",
+ "The total number of combinations of selected faculties are 27720\n"
+ ]
+ }
+ ],
+ "source": [
+ "def combination(n,r): #Function definition\n",
+ " i=n\n",
+ " numerator=1\n",
+ " denominator=1\n",
+ " for i in range((n-r)+1,n+1): #computation of the numerator\n",
+ " numerator=numerator*i\n",
+ " for j in range (1,r+1): #computation of the denominator\n",
+ " denominator=denominator*j\n",
+ " result=numerator/denominator\n",
+ " return result\n",
+ "num1=input(\"Enter the total number of faculty in computer science department\")\n",
+ "comb1=input(\"Enter the number of faculty to be selected for computer science department\")\n",
+ "num2=input(\"Enter the total number of faculty in maths department\")\n",
+ "comb2=input(\"Enter the number of faculty to be selected for maths department\")\n",
+ "\n",
+ "print \"The total number of combinations of selected faculties are \",combination(num1,comb1)*combination(num2,comb2) #Function call\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_1.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_1.ipynb
new file mode 100644
index 00000000..485e6b32
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/chapter6_1.ipynb
@@ -0,0 +1 @@
+{"nbformat_minor": 0, "cells": [{"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "n=2 #number of employees\nr=12 #number of office rooms\nways_alloc_sanchez=12\nways_alloc_patel=11\n\n#By PRODUCT RULE\nprint \"Total ways to assign offices to these employees is\",ways_alloc_sanchez*ways_alloc_patel\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Total ways to assign offices to these employees is 132\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 02: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "alphabets=26 #Total number of alphabets\nposint=100 #Total positive numbers not beyond 100\n\n#number of chairs to be labelled with a alphabet and an integer using product rule\nprint \"Total number of chairs that can be labelled with an alphabet and an integer is\",alphabets*posint\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Total number of chairs that can be labelled with an alphabet and an integer is 2600\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 03: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "mc=32 #total number of microcomputers\nport=24 #total number of ports in each microcomputer\n\n#total number of different ports to a microcomputer in the center are found using product rule\n\nprint \"total number of ports\",mc*port\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "total number of ports 768\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 386", "cell_type": "markdown", "metadata": {}}, {"execution_count": 4, "cell_type": "code", "source": "bits=2 #possible bits either 0 or 1\nns=7 #number of bits in the string (ie). length of the string\n # 7 bits are capable of taking either 0 or 1 so by PRODUCT RULE\nprint \"Total different bit strings of lenth seven are\",bits**ns\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Total different bit strings of lenth seven are 128\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 05: Page 387", "cell_type": "markdown", "metadata": {}}, {"execution_count": 7, "cell_type": "code", "source": "letters=26 #number of letters in english alphabet\nno_of_letters=3 #number of letters \nchoices=10 #number of choices for each letter\nresult=1#in order to avoid junk values. Assigned it to 1.\nfor i in range(0,no_of_letters):\n result=result*letters*choices\nprint \"The total number of choices are\",result", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The total number of choices are 17576000\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 01: Page 407", "cell_type": "markdown", "metadata": {}}, {"execution_count": 10, "cell_type": "code", "source": "def permutation(n,r): #function definition\n \n i=n\n result=1\n for i in range((n-r)+1,n+1): #computing the permutation\n result=result*i\n \n return result\n\nprint \"The number of ways to select 3 students from a group of 5 students to line up for a picture is \",permutation(5,3) #function call\nprint \"The number of ways to select 5 students from a group of 5 students to line up for a picture is \",permutation(5,5) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "The number of ways to select 3 students from a group of 5 students to line up for a picture is 60\nThe number of ways to select 5 students from a group of 5 students to line up for a picture is 120\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 04: Page 409", "cell_type": "markdown", "metadata": {}}, {"execution_count": 1, "cell_type": "code", "source": "def permutation(n,r): #function definition\n \n i=n\n result=1\n for i in range((n-r)+1,n+1): #permutation computation\n result=result*i\n return result\nnum=input(\"Enter the number of people\")\nperm=input(\"Enter the prizes\")\nprint \"The number of ways to decide the prize winners is\",permutation(num,perm) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of people100\nEnter the prizes3\nThe number of ways to decide the prize winners is 970200\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 05: Page 409", "cell_type": "markdown", "metadata": {}}, {"execution_count": 2, "cell_type": "code", "source": "def permutation(n,r):\n \n i=n\n result=1\n for i in range((n-r)+1,n+1):\n result=result*i\n \n return result\nnum=input(\"Enter the number of runners\")\nperm=input(\"Enter the number of prizes\")\nprint \"The number of ways to decide the prize winners is\",permutation(num,perm)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of runners8\nEnter the number of prizes3\nThe number of ways to decide the prize winners is 336\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 06: Page 409", "cell_type": "markdown", "metadata": {}}, {"execution_count": 3, "cell_type": "code", "source": "def calc(n):\n \n i=n\n result=1\n for i in range(1,n): #find the number of ways to decide the path. since the first city us decided. The for loop is from 1 to n\n result=result*i\n \n return result\nnum=input(\"Enter the number of cities\") \nprint \"The number of possible ways to decide the path is\",calc(num)\n\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of cities8\nThe number of possible ways to decide the path is 5040\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 10: Page 410", "cell_type": "markdown", "metadata": {}}, {"execution_count": 5, "cell_type": "code", "source": "def combination(n,r): #combination function\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1):#computes the value of the numerator \n numerator=numerator*i\n for j in range (1,r+1): #computes the value of the denominator\n denominator=denominator*j\n result=numerator/denominator #computes result\n return result\nnum=input(\"Enter the number of elements\")\ncomb=input(\"Enter the combinations\")\nprint \"The number of combinations are \",combination(num,comb)\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of elements4\nEnter the combinations2\nThe number of combinations are 6\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 12: Page 412", "cell_type": "markdown", "metadata": {}}, {"execution_count": 7, "cell_type": "code", "source": "def combination(n,r): #function definition for combination\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1):\n numerator=numerator*i\n for j in range (1,r+1):\n denominator=denominator*j\n result=numerator/denominator\n return result\nnum=input(\"Enter the number of members in a team\")\ncomb=input(\"Enter the number of players\")\nprint \"The number of combinations are \",combination(num,comb) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the number of members in a team10\nEnter the number of players5\nThe number of combinations are 252\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 13: Page 412", "cell_type": "markdown", "metadata": {}}, {"execution_count": 8, "cell_type": "code", "source": "def combination(n,r): #function definition\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1):\n numerator=numerator*i\n for j in range (1,r+1):\n denominator=denominator*j\n result=numerator/denominator\n return result\nnum=input(\"Enter the total number of astronauts\")\ncomb=input(\"Enter the number of astronauts to be selected \")\nprint \"The total number of combinations of selected astronauts to Mars are \",combination(num,comb) #function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the total number of astronauts30\nEnter the number of astronauts to be selected 6\nThe total number of combinations of selected astronauts to Mars are 593775\n"}], "metadata": {"collapsed": false, "trusted": true}}, {"source": "#Chapter 06: Counting", "cell_type": "markdown", "metadata": {}}, {"source": "## Example 15: Page 413", "cell_type": "markdown", "metadata": {}}, {"execution_count": 9, "cell_type": "code", "source": "def combination(n,r): #Function definition\n i=n\n numerator=1\n denominator=1\n for i in range((n-r)+1,n+1): #computation of the numerator\n numerator=numerator*i\n for j in range (1,r+1): #computation of the denominator\n denominator=denominator*j\n result=numerator/denominator\n return result\nnum1=input(\"Enter the total number of faculty in computer science department\")\ncomb1=input(\"Enter the number of faculty to be selected for computer science department\")\nnum2=input(\"Enter the total number of faculty in maths department\")\ncomb2=input(\"Enter the number of faculty to be selected for maths department\")\n\nprint \"The total number of combinations of selected faculties are \",combination(num1,comb1)*combination(num2,comb2) #Function call\n", "outputs": [{"output_type": "stream", "name": "stdout", "text": "Enter the total number of faculty in computer science department9\nEnter the number of faculty to be selected for computer science department3\nEnter the total number of faculty in maths department11\nEnter the number of faculty to be selected for maths department4\nThe total number of combinations of selected faculties are 27720\n"}], "metadata": {"collapsed": false, "trusted": true}}], "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"}}}} \ No newline at end of file
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch2.png b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch2.png
new file mode 100644
index 00000000..9f76c123
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch2.png
Binary files differ
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch2_1.png b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch2_1.png
new file mode 100644
index 00000000..9f76c123
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch2_1.png
Binary files differ
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch4.png b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch4.png
new file mode 100644
index 00000000..6d747624
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch4.png
Binary files differ
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch4_1.png b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch4_1.png
new file mode 100644
index 00000000..6d747624
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch4_1.png
Binary files differ
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch5.png b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch5.png
new file mode 100644
index 00000000..c5cb06bb
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/ch5.png
Binary files differ
diff --git a/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/chapter5.ipynb b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/chapter5.ipynb
new file mode 100644
index 00000000..14960493
--- /dev/null
+++ b/Discrete_Mathematics_and_its_Applications_by_Kenneth_H.Rosen/screenshots/chapter5.ipynb
@@ -0,0 +1,312 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 346"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "The value of f( 1 ) is 9\n",
+ "The value of f( 2 ) is 21\n",
+ "The value of f( 3 ) is 45\n",
+ "The value of f( 4 ) is 93\n"
+ ]
+ }
+ ],
+ "source": [
+ "#to compute the recursive functions\n",
+ "def f(n):\n",
+ "\n",
+ " if n==0:\n",
+ " return 3\n",
+ " else:\n",
+ " n=n-1\n",
+ " result=2*f(n)+3 #recursive call\n",
+ " return result\n",
+ "for num in range(1,5):\n",
+ " r=f(num)\n",
+ " print \"The value of f(\",num,\") is\",r #Prints the result for individual instance\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 01: Page 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter a number whose factorial is to be found4\n",
+ "The factorial of 4 is 24\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute the factorial of a given number using recursion\n",
+ "def factorial(n):\n",
+ " if n==0:\n",
+ " return 1\n",
+ " else:\n",
+ " return n*factorial(n-1) #recursive function call\n",
+ "num=input(\"Enter a number whose factorial is to be found\");\n",
+ "print \"The factorial of\",num,\"is\",factorial(num);\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 02: Page 361"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number5\n",
+ "Enter the power4\n",
+ "The value of 5 to the power 4 is 625\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute power using recursive algorithm\n",
+ "def power(a,n):\n",
+ " if n==0:\n",
+ " return 1\n",
+ " else:\n",
+ " return a*power(a,n-1) #recursive call algorithm\n",
+ "num=input(\"Enter the number\");\n",
+ "p=input(\"Enter the power\");\n",
+ "print \"The value of\",num,\"to the power\",p,\"is\",power(num,p);\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 03: Page 362"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the first number5\n",
+ "Enter the second number8\n",
+ "The gcd of 5 , 8 is 1\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute gcd using modular recursion\n",
+ "def gcd(a,b):\n",
+ " if a==0:\n",
+ " return b\n",
+ " else:\n",
+ " return gcd(b%a,a) #recursive call\n",
+ "\n",
+ "num1=input(\"Enter the first number\")\n",
+ "num2=input(\"Enter the second number\")\n",
+ "print \"The gcd of\",num1,\",\",num2,\"is\",gcd(num1,num2)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 04: Page 362"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Enter the number2\n",
+ "Enter the power5\n",
+ "Enter the modulo number3\n",
+ "The answer of mpower( 2 , 5 , 3 ) is 2\n"
+ ]
+ }
+ ],
+ "source": [
+ "#To compute mpower function using recursion\n",
+ "def mpower(b,n,m):\n",
+ " if n==0:\n",
+ " return 1\n",
+ " else:\n",
+ " if n%2==0:\n",
+ " return ((mpower(b,n/2,m))**2) % m #recursive call\n",
+ " else:\n",
+ " return ((mpower(b,n/2,m)**2)%m*(b%m))%m #recursive call\n",
+ "number=input(\"Enter the number\")\n",
+ "power=input(\"Enter the power\")\n",
+ "modulo=input(\"Enter the modulo number\");\n",
+ "print \"The answer of mpower(\",number,\",\",power,\",\",modulo,\") is\",mpower(number,power,modulo)\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# 05:INDUCTION AND RECURSION"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 09: Page 367"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "enter the numbers to be merge sorted8,2,4,6,9,7,10,1,5,3\n",
+ "Ther Merge sort is ['1', '10', '2', '3', '4', '5', '6', '7', '8', '9']\n"
+ ]
+ }
+ ],
+ "source": [
+ "def msort2(x): #function for merge sort\n",
+ " if len(x) < 2:\n",
+ " return x\n",
+ " result = [] \n",
+ " mid = int(len(x)/2) #divides the elements into halves\n",
+ " y = msort2(x[:mid])\n",
+ " z = msort2(x[mid:])\n",
+ " while (len(y) > 0) and (len(z) > 0):\n",
+ " if y[0] > z[0]:\n",
+ " result.append(z[0]) #merges to append the elements\n",
+ " z.pop(0)\n",
+ " else:\n",
+ " result.append(y[0])\n",
+ " y.pop(0)\n",
+ " result += y\n",
+ " result += z\n",
+ " return result\n",
+ "l=[]\n",
+ "r=[]\n",
+ "l=raw_input(\"enter the numbers to be merge sorted\").split(\",\")\n",
+ "r=msort2(l)\n",
+ "print \"Ther Merge sort is\", r\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 2",
+ "language": "python",
+ "name": "python2"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 2
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython2",
+ "version": "2.7.9"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}