Assignment 8 Solutions - Discrete Structures | CS 173, Assignments of Discrete Structures and Graph Theory

Material Type: Assignment; Class: Discrete Structures; Subject: Computer Science; University: University of Illinois - Urbana-Champaign; Term: Fall 2006;

Typology: Assignments

Pre 2010

Uploaded on 03/16/2009

koofers-user-q3h-1
koofers-user-q3h-1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS173 Discrete Mathematical Structures
Fall 2006
Homework #8
due Tuesday, October 31, 2006, 8:00 a.m.
(36 points + 4 bonus points)
1. Recursive Definition (6 points)
Early members of the Pythagorean Society defined figurate numbers to be the number of dots
in certain geometrical configurations.
a. The first four triangular numbers are 1, 3, 6, and 10. (3 points)
1 3 6 10
Find a recursive expression for the nth triangular number. What is the value of the 10
th
triangular number?
b. The first four pentagonal numbers are 1, 5, 12, and 22. (3 points)
1 5 12 22
Find a recursive expression for the nth pentagonal number. What is the value of the 7
th
pentagonal number?
pf3
pf4

Partial preview of the text

Download Assignment 8 Solutions - Discrete Structures | CS 173 and more Assignments Discrete Structures and Graph Theory in PDF only on Docsity!

CS173 Discrete Mathematical Structures

Fall 2006

Homework

due Tuesday, October 31, 2006, 8:00 a.m.

(36 points + 4 bonus points)

  1. Recursive Definition (6 points) Early members of the Pythagorean Society defined figurate numbers to be the number of dots in certain geometrical configurations. a. The first four triangular numbers are 1, 3, 6, and 10. (3 points)

Find a recursive expression for the nth triangular number. What is the value of the 10th triangular number?

b. The first four pentagonal numbers are 1, 5, 12, and 22. (3 points)

Find a recursive expression for the nth pentagonal number. What is the value of the 7th pentagonal number?

  1. Recursive Algorithm (8 points) ========= choose a format for your pseudocode ==========

Consider following two formats to write a recursive algorithm for computing an^ where a is a

nonzero real number and n is a nonnegative integer. Format 1:

Format 2:

Choose one of these formats to write pseudocode for the following questions.

a. Devise a recursive algorithm to find xn^ mod m when n, x, and m are positive integers

based on the fact that xn^ mod m = (xnโˆ’^1 mod m โ‹… x mod m) mod m. (4 points)

b. Devise a recursive algorithm to evaluate the nth term of the following sequence S:

p, p โˆ’ q, p + q, p โˆ’ 2 q, p + 2 q, p โˆ’ 3 q, p + 3 q,... (4 points)

procedure power(a: nonzero real number, n: nonnegative integer)

if n = 0 then power(a, n) := 1

else power(a,n) := a โ‹… power(a, n โˆ’ 1)

algorithm power(nonzero real number a; nonnegative integer n) if n = 0 then

power(a, n) := 1

else

power(a, n) := a โ‹… power(a, n โˆ’ 1)

end if end algorithm power

c. Let A be an n ร— n matrix, and suppose A[i, j] contains the value in row i column j of the

matrix. Assume that the value of A[i, j] can be accessed in constant time. What does the following algorithm do? Give a tight upper bound for the running time of this algorithm. (3 points)

d. (Bonus points for this one!! Your total homework score will not be hurt if you skip this problem.) S is an array of integers {1, 2, 3, โ€ฆ, n} (each number appears in S exactly once). Subroutine swap(S[a], S[b]) exchanges array items indexed by a and b. Each swap takes constant time. What does the following algorithm do? What is the worst case time complexity? Why? (4 points)

algorithm X(array S, nonnegative integer n) for i = 1 to n -1 do

while S[i] โ‰  i do

swap(S[i], S[S[i]]) end while end for end algorithm X

algorithm MatrixSum(matrix A) sum := 0 for k = 1 to n do for j = k to n do sum := sum + A[j, k] end for end for return sum end algorithm MatrixSum