summaryrefslogtreecommitdiff
path: root/Discrete_Mathematics_by_S_Lipschutz/3-Functions_and_Algorithms.ipynb
blob: 82c3aae675d49f32cf45e7851a81320db7304016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
{
"cells": [
 {
		   "cell_type": "markdown",
	   "metadata": {},
	   "source": [
       "# Chapter 3: Functions and Algorithms"
	   ]
	},
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 3.10: Polynomial_evaluation.sci"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"x = poly(0, 'x');\n",
"p = 2*x^3-7*x^2+4*x-15;\n",
"disp(p,'the polynomial is')\n",
"k=horner(p,5);\n",
"disp(k,'value of the polynomial at x=5 is')"
   ]
   }
,
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 3.11: Greatest_Common_Divisor.sci"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"V=int32([258,60]);\n",
"thegcd=gcd(V);\n",
"disp(thegcd,'the gcd of the two numbers 258 and 60 is')"
   ]
   }
,
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 3.8: Recursively_defined_functions.sci"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"function[k]=fact(a)\n",
"k=-1;\n",
"if(a<0|a>200)\n",
"disp('Invalid');\n",
"break;\n",
" else\n",
"if(a==1|a==0)\n",
"k=1;\n",
" else\n",
"k=a*fact(a-1);\n",
"end\n",
"end\n",
"endfunction\n",
"a=4;\n",
"p=fact(a);\n",
"disp(p,'the value of 4! is')"
   ]
   }
,
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 3.9: Cardinality.sci"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"x=1;\n",
"y=2;\n",
"z=3;\n",
"A=[x,y,z];\n",
"disp('cardinality of set A is:')\n",
"length(A)  \n",
"B=[1,3,5,7,9]\n",
"disp('cardinality of set B is:')\n",
"length(B)\n",
"\n",
"// 3.9 (b)\n",
"disp('the set E has the following elements)\n",
"E=[2,4,6 %inf]  //set E is the set of all positive even numbers and N is the set of all natural numbers\n",
"disp('function f:N to E is defined.So,E has the same cardinality as N')\n",
"disp('set E is countably infinite:')\n",
"for x=2:2:%inf\n",
"y=2*x;\n",
"disp(y)\n",
"end"
   ]
   }
],
"metadata": {
		  "kernelspec": {
		   "display_name": "Scilab",
		   "language": "scilab",
		   "name": "scilab"
		  },
		  "language_info": {
		   "file_extension": ".sce",
		   "help_links": [
			{
			 "text": "MetaKernel Magics",
			 "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
			}
		   ],
		   "mimetype": "text/x-octave",
		   "name": "scilab",
		   "version": "0.7.1"
		  }
		 },
		 "nbformat": 4,
		 "nbformat_minor": 0
}