1)



Fall 2005 COP 3530 Exam #1

Date: 9/22/05

1) (8 pts) What is the running time of the following method below that calculates the nth Fibonacci number in terms of n? Assume that the addition of two k-bit numbers takes O(k) time and that Fk is represented using k bits.

public BigInteger Fibonacci(int n) {

BigInteger one = new BigInteger(“1”);

fib1 = one;

fib2 = one;

for (int i=2; i 0) {

for (j=0; j 2, T(1) = 6, T(2) = 4

5) (3 pts) Determine a loop invariant for the loop below right before the first statement inside of the loop is evaluated during each iteration. (Your invariant should be about the value of the variable sum in terms of cnt.)

int sum = 0, i = 1, cnt = 1;

while (cnt ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download