Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
santos-discrete_math_lecture_notes_2.pdf
Скачиваний:
23
Добавлен:
16.03.2016
Размер:
691.48 Кб
Скачать

10

 

 

 

 

 

 

 

 

 

 

 

Chapter 1

 

 

 

 

 

 

 

 

 

 

 

 

 

Algorithm 1.6.6: ISPRIME2(n)

 

 

 

 

 

 

 

 

8

 

 

 

 

 

 

 

 

 

 

 

 

comment: n is a positive integer.

 

 

 

 

 

 

if n = 1

 

8

 

 

 

 

 

 

 

 

 

 

then output (n is a unit.)

 

 

 

 

 

 

 

 

if n = 2

 

 

 

 

 

 

 

 

 

 

 

 

then output (n is prime.)

 

 

 

 

 

 

 

 

if n = 3

 

 

 

 

 

 

 

 

 

 

 

 

then output (n is prime.)

 

 

 

 

 

 

 

 

if n > 3

 

 

 

 

 

 

 

 

 

 

 

 

<if n mod 2 = 0

 

 

 

 

 

 

 

 

 

then output (n is even. Its smallest factor is 2.)

 

 

 

 

<flag true

 

 

 

 

 

 

 

 

 

 

 

i 1

 

 

 

 

 

 

 

 

 

 

 

 

8<= b

 

 

 

 

=

 

 

 

 

 

 

 

 

 

c

 

 

 

 

 

 

 

while i

n

 

and flag

 

true

 

 

then :>

 

 

 

<i i + 2

 

 

 

 

 

 

 

else

:>

do

:if n mod i = 0

 

 

 

 

 

 

 

¦

flag false

 

 

 

 

 

 

 

then

 

 

 

 

 

 

if flag = true

 

 

 

 

 

 

 

 

 

then output (n is prime.)

 

 

 

 

 

 

else output (Not prime. n smallest factor is i.)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Homework

8

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

36 Problem What will the following algorithm return for n = 5? You must trace the algorithm carefully, outlining all your steps.

 

 

 

 

Algorithm 1.6.7: MYSTERY(n)

 

 

 

 

 

x 0

 

 

 

 

i 1

 

 

 

 

while n > 1

 

 

 

 

 

<if n i > 4

 

 

 

 

 

then x x + 2n

 

 

 

do :> else x x + n

 

 

 

 

n n 2

 

 

 

 

 

i i + 1

 

 

 

 

 

8

 

 

 

 

return (x)

 

 

 

 

 

 

37 Problem What will the following algorithm return for n = 3?

 

 

 

 

Algorithm 1.6.8: MYSTERY(n)

 

 

 

 

 

x 0

 

 

 

 

while n > 0

 

 

 

 

 

<for i § 1 to n

 

 

 

 

do :> do for j i to n

 

 

 

 

do

x i j + x

 

 

 

 

n n 1

 

 

 

 

return (x)

 

 

 

 

 

 

10

Answers

11

38 Problem Assume that the division operator / acts as follows on the integers: if the division is not even, a/b truncates the decimal part of the quotient. For example 5/2 = 2, 5/3 = 1. Assuming this write an algorithm that reverses the digits of a given integer. For example, if 123476 is the input, the output should be 674321. Use only one while loop, one mod operation, one multiplication by 10 and one division by 10.

39 Problem Given is an array of length m + n, which is sorted in increasing order:

X [0] < X [1] < . . . < X [m 1] < X [m] < . . . < X [m + n 1].

Without using another array reorder the array in the form

X [m] → X [m + 1] → . . . → X [m + n 1] → X [0] → X [1] → . . . → X [m 1].

Do this using algorithm REVERSEARRAY from example 23 a few times.

40 Problem The Fibonacci Sequence is defined recursively as follows:

f0 = 0; f1 = 1, f2 = 1, fn+1 = fn + fn1, n 1.

Write an algorithm that finds the n-th Fibonacci number.

41 Problem Write an algorithm which reads a sequence of real numbers and determines the length of the longest non-decreasing subsequence. For instance, in the sequence

7, 8, 7, 8, 9, 2, 1, 8, 7, 9, 9, 10, 10, 9,

the longest non-decreasing subsequence is 7, 9, 9, 10, 10, of length 5.

42 Problem Write an algorithm that reads an array of n integers and finds the second smallest entry.

43 Problem A partition of the strictly positive integer n is the number of writing n as the sum of strictly positive summands, without taking the order of the summands into account. For example, the partitions of 4 are (in “alphabetic order” and with the summands w ritten in decreasing order)

1 + 1 + 1 + 1; 2 + 1 + 1; 3 + 1; 2 + 2; 4.

Write an algorithm to generate all the partitions of a given integer n.

Answers

36

In the first turn around the loop, n = 5, i = 1, n i > 4 and thus x = 10. Now n = 3, i = 2, and we go a second turn around the loop. Since

n i > 4, x = 10 + 2 3 = 16. Finally, n = 1, i = 3, and the loop stops. Hence x = 16 is returned.

38

Here is a possible approach.

 

 

 

 

 

 

 

 

Algorithm 1.6.9: REVERSE(n)

 

 

 

 

 

 

 

comment: n is a positive integer.

 

 

 

 

x 0

 

 

 

 

 

while8n 6= 0

 

 

 

 

 

<comment: x accumulates truncated digit.

 

 

 

 

do :>x x 10 + n

mod 10

 

 

 

 

comment: We now truncate a digit of the input.

 

 

 

 

n n/10

 

 

 

 

 

return (x)

 

 

 

 

 

 

 

 

 

 

 

39 Reverse the array first as

X [m + n 1] > X [m + n 2] > . . . > X [m] > X [m 1] > . . . > X [0].

Then reverse each one of the two segments:

X [m] → X [m + 1] → . . . → X [m + n 1] → X [0] → X [1] → . . . → X [m 1].

11

12

 

 

 

§

 

 

 

 

 

 

Chapter 1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

40

Here is a possible solution.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Algorithm 1.6.10: FIBONACCI(n)

 

 

 

 

 

 

 

if n = 0

 

 

 

 

 

 

 

 

 

 

 

then return (0)

 

 

 

 

 

 

 

 

 

else

last 0

 

 

 

 

 

 

 

 

 

 

 

current 1

 

 

 

 

 

 

 

for( i 2 to n

 

 

 

 

 

 

 

 

 

 

 

temp last + current

 

 

 

 

 

 

 

 

last current

 

 

 

 

 

 

 

 

 

 

current temp

 

 

 

 

 

 

 

 

return (current)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

8

 

8

 

 

 

 

41

Assume that the data is

 

 

 

f . eof means “end of file.” newEl and oldEl are the current and

 

the previous elements. d is

read from some file

 

the length of the current run of non-decreasing numbers. dMax is the length of the longest run.

 

 

 

 

 

 

11:

 

 

d 1

 

 

 

 

 

 

 

 

 

 

 

 

 

Algorithm 1.6.

 

LARGESTINCREASINGSEQUENCE( f )

 

 

 

 

1 d

 

 

 

 

 

 

 

 

 

 

1 dMax

 

 

 

 

 

 

 

 

 

while not eof

 

 

 

 

 

 

 

 

 

 

 

<if newEl >= oldEl

 

 

 

 

 

 

 

 

<d d

(+ 1

 

 

 

 

 

 

do :> then > else

 

if d > dMax

 

 

 

 

 

 

then dMax d

 

 

 

 

if d > dMax

oldEl newEL

 

 

 

 

 

 

 

 

 

 

 

42

 

then dMax d

 

 

 

 

 

 

 

 

 

 

 

Here is one possible approach.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Algorithm 1.6.12: SECONDSMALLEST(n, X )

 

 

 

 

 

 

 

comment: X is an array of length n.

 

 

 

 

second x[0]

 

 

 

 

 

 

 

 

 

minimum second

 

 

 

 

 

 

 

for i 8

0 to n 1

 

 

 

 

 

 

 

 

 

 

<if minimum = second

 

 

 

 

 

 

 

 

(if X [i] < minimum

 

 

 

 

 

 

 

then

8

then minimum X [i]

 

 

 

 

 

 

 

 

else second X [i]

 

 

 

 

 

 

 

 

<if X [i] < minimum

 

 

 

 

 

do :>

 

 

then

 

§

 

 

 

 

 

 

 

 

 

 

 

second minimum

 

 

 

 

 

 

 

else :>

 

§ minimum X [i]

 

 

 

 

 

 

 

 

 

else

 

if X [i] > minimum and X [i] < second

 

 

 

 

 

 

 

 

 

 

then second X [i]

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

43 We list partitions of n in alphabetic order and with decreasing summands. We store them in an array of length n + 1 with X [0] = 0.. The length of the partition is k and the summands are X [1] + ···+ X [k]. Initially k = n and X [1] = ··· = X [n] = 1. At the end we have X [1] = n and the rest are 0.

12

Answers

 

13

 

 

 

 

Algorithm 1.6.13: PARTITIONS(n)

 

 

 

 

s k 1

 

 

while

not ((s = 1) or (X [s 1] > X [s]))

 

 

s s 1

 

 

X [s] ← X [s] + 1

 

 

sum 0

 

 

for i s + 1 to k

 

 

sum sum + X [i]

 

 

for i 1 to sum 1

 

 

X [s + i] ← 1

 

 

k s + sum 1

 

 

 

 

 

 

13

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]