Sie sind auf Seite 1von 14

Integer Arithmetic

Appendix B Discrete-Event Simulation: A First Course

Appendix B: Integer Arithmetic

Discrete-Event Simulation

c 2006 Pearson Ed., Inc.

0-13-142917-5

1/ 14

Appendix B: Integer Arithmetic


Terminology The positive integers are 1, 2, 3, . . . The nonnegative integers are 0, 1, 2, 3, . . . The negative integers are . . . , 3, 2, 1 The integers are . . . , 3, 2, 1, 0, 1, 2, 3, . . . Properties of the positive integers Well ordering: any nonempty set of positive integers has a smallest element Mathematical induction: for a set of positive integers S , if 1 S and if n S n + 1 S , then all positive integers are in S

Appendix B: Integer Arithmetic

Discrete-Event Simulation

c 2006 Pearson Ed., Inc.

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

If (a, b ) = (7, 17) then (q , r ) = (3, 4) Note 17 = 7 (3) + 4

Appendix B: Integer Arithmetic

Discrete-Event Simulation

c 2006 Pearson Ed., Inc.

0-13-142917-5

3/ 14

Floor and Ceiling


Denition B.1 For any realvalued number x , the oor of x , x , is the largest integer n such that n x
x x

the ceiling of x , x , is the smallest integer n such that x n


x

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

Appendix B: Integer Arithmetic

Discrete-Event Simulation

c 2006 Pearson Ed., Inc.

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

Appendix B: Integer Arithmetic

Discrete-Event Simulation

c 2006 Pearson Ed., Inc.

0-13-142917-5

6/ 14

More ANSI C concerns

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;

Appendix B: Integer Arithmetic

Discrete-Event Simulation

c 2006 Pearson Ed., Inc.

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

Appendix B: Integer Arithmetic

Discrete-Event Simulation

c 2006 Pearson Ed., Inc.

0-13-142917-5

8/ 14

Divisors and Primes


Denition B.3 If b mod a = 0, so that b = aq , then a is said to be a divisor of b . Equivalently, a is said to divide b . Example B.6 If a divides b and c , then
a divides b + c if b > c , then a divides b c

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.

Appendix B: Integer Arithmetic

Discrete-Event Simulation

c 2006 Pearson Ed., Inc.

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

Greatest Common Divisor


Denition B.5 If a, b are positive integers, then positive integer d is a common divisor i d divides both a and b . The largest such d is called the greatest common divisor, denoted gcd(a, b ) = gcd(b , a) Denition B.6 Positive integers a, b are relatively prime i gcd(a, b ) = 1 Euclidean Algorithm to compute gcd(a, b ) r = a % b; while (r > 0) { a = b; b = r; r = a % b; } return b ;
Appendix B: Integer Arithmetic Discrete-Event Simulation c 2006 Pearson Ed., Inc. 0-13-142917-5

11/ 14

Fermats little theorem

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.

Appendix B: Integer Arithmetic

Discrete-Event Simulation

c 2006 Pearson Ed., Inc.

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 ... ... ... ... ...

Appendix B: Integer Arithmetic

Discrete-Event Simulation

c 2006 Pearson Ed., Inc.

0-13-142917-5

13/ 14

Seive of Eratosthenes: Implementation

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;

Appendix B: Integer Arithmetic

Discrete-Event Simulation

c 2006 Pearson Ed., Inc.

0-13-142917-5

14/ 14

Das könnte Ihnen auch gefallen