Escolar Documentos
Profissional Documentos
Cultura Documentos
Discrete-Event Simulation
0-13-142917-5
1/ 14
Discrete-Event Simulation
0-13-142917-5
2/ 14
Integer Division
Theorem (B.1: Division Theorem) If b is an integer and a is a positive integer, then there exists a unique pair of integers q , r with 0 r < a such that b = aq + r The theorem does not say how to determine (q , r ). If (a, b ) = (7, 17) then (q , r ) = (2, 3) Note 17 = 7 2 + 3
Discrete-Event Simulation
0-13-142917-5
3/ 14
Properties of oor and ceiling: If x is an integer, then x = x = x x = x If x is not an integer, then x = x + 1 x x < x + 1
If n is an integer, then x + n = x + n
Appendix B: Integer Arithmetic Discrete-Event Simulation c 2006 Pearson Ed., Inc. 0-13-142917-5
4/ 14
ANSI C concerns
A oatingpoint value is converted to an integer value by truncation of any fractional part. If x is type double and n is type long, the assignment n = (long) x produces n = x if x 0.0 n = x if x 0.0 The math library <math.h> provides functions double floor(double x ) double ceil(double x ) which are correct even if x is negative
Discrete-Event Simulation
0-13-142917-5
5/ 14
Modulus
Denition B.2 Relative to the division theorem, if a is a positive integer and b is an integer, then the remainder is r = b b /aa Equivalently, the modulus (mod) function gives the remainder r = b mod a = b b /aa the quotient is q = b /a
Discrete-Event Simulation
0-13-142917-5
6/ 14
If a is a positive integer and b is a nonnegative integer, q and r can be computed in ANSI C as q = b / a; // q is b /a r = b % a; // r is b mod a If b or a is a negative integer, ANSI C allows q and r to be implementation dependent, with (b/a)*a + (b%a) == b // Intel // q = -2 // r = -3 Theory q = -3 r = 4
q = -17 / 7; r = -17 % 7;
Discrete-Event Simulation
0-13-142917-5
7/ 14
Modulus Properties
Theorem (B.2) If a is a positive integer, and b1 , b2 , . . . , bn are integers, then (b1 + b2 + + bn ) mod a = ((b1 mod a) + (b2 mod a) + + (bn mod a)) mod a (b1 b2 bn ) mod a = ((b1 mod a)(b2 mod a) (bn mod a)) mod a
If a is a positive integer, and b , c are integers, then (b + ac ) mod a = ((b mod a) + (ac mod a)) mod a = ((b mod a) + ((a mod a)(c mod a)) mod a) mod a = (b mod a) mod a = b mod a
Discrete-Event Simulation
0-13-142917-5
8/ 14
if a divides b or c , then a divides bc if a divides b and b divides c , then a divides c Denition B.4 A positive integer p > 1 is prime i the only positive integers that divide p are p and 1.
Discrete-Event Simulation
0-13-142917-5
9/ 14
Prime Factorization
Theorem (B.3: Fundamental Theorem of Arithmetic) Any positive integer n > 1 can be uniquely written as
k1 k2 kr n = p1 p2 pr
where p1 < p2 < < pr are r distinct primes with exponents k1 > 0, . . . , kr > 0.
38 = 2 19 39 = 3 13 40 = 23 5 41 = 41 (prime)
42 = 2 3 7
Appendix B: Integer Arithmetic Discrete-Event Simulation c 2006 Pearson Ed., Inc. 0-13-142917-5
10/ 14
11/ 14
Theorem (B.4) If m is prime and a is an integer such that a/m is not an integer then am1 mod m = 1 Theorem (B.5) If m is prime, then am mod m = a mod m for all integers a.
Discrete-Event Simulation
0-13-142917-5
12/ 14
Seive of Eratosthenes
Finds all primes between 2 and N for specied N . Does so by crossing out multiples of primes: 2 2 2 2 2 14 14 14 14 14 Stop when pointer (underlined number) passes N Remaining integers are prime 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 9 9 9 9 9 10 10 10 10 10 11 11 11 11 11 12 12 12 12 12 13 13 13 13 13 15 15 15 15 15 ... ... ... ... ...
Discrete-Event Simulation
0-13-142917-5
13/ 14
Uses an array of binary values (0 for composite, 1 for prime) of dimension N + 1 Algorithm B.2 prime[0] = 0; // zero is composite prime[1] = 0; // one is composite for (n = 2; n <= N ; n++) prime[n] = 1; for (n = 2; n <= N ; n++) if (prime[n]) for (s = 2; s <= (N /n); s ++) prime[s * n] = 0;
Discrete-Event Simulation
0-13-142917-5
14/ 14