Question

1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci number. By definition Fibonnaci(0) is 1, Fibonnaci(1) is 1, Fibonnaci(2) is 2, Fibonnaci(3) is 3, Fibonnaci(4) is 5, and so on. Your function may only use a constant amount of memory (i.e. no auxiliary array). Argue that the running time of the function is Θ(n), i.e. the function is linear in n. 2. (10 points) Order the following functions by growth rate: N, \N, N1.5, N2, N log N, N log logN, N log2 N, N log (N2), 2/N, 2V, 2N/2, 37, N2 log N, N3. Indicate which functions grow at the same rate. 3. (10 points) Compute the running time T(n) of the program fragment below and provide an analysis of the running time (Big-Oh notation will do). For convenience, assume that operations inside for loops take constant time, i.e. Θ(1) sum = 0 for( i-0; i<n; i++) for j0; 3 for (k=0; sum++ k 〈 j, k++) 4. (10 points) An algorithm takes 0.5ms for input size 100. How long will it take for input size 500 if the running time is the following (assume low-order terms are negligible): linear, O(n log n), quadratic, and cubic? 5. (10 points) Give an O(logN) algorithm to determine if there exists an integer i such that Ai-i in an array of integers A1くA·Ag < … < AN. For example, in {-10,-3, 3, 5, 7}, A,-3. In {2, 3, 4, 5, 6, 7), there 1s no such 6. (10 points) Show that X62, can be computed with only eight multiplications. 7. (10 points) Write a recursive version of the binary search function to find if x is in A 8. (10 points) Given two functions T1(n) = 20n and T2 (n) = n2 + 10n-200 and assuming integer n > 0, what is the minimum value of n such that T1 (n) < T2 (n)? 9. (10 points) Is a linear-time algorithm always faster than a quadratic-time algorithm? Why or why not? 10. (10 points) Explain why recursion should never be used as a substitute for a simple for loop?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you post ore than 1 question, then as HOMEWORKLIB RULES guidelines I just have to answer one question.

1.

function Fibonnaci(n):

{

    // first term of series

    if n == 0

        return 1

   

    if n == 1

        return 2

    a = 1

    b = 2

    for i <- 2 to n

    {

        c = a + b

        a = b

        b = c

    }

    return b

}

The above function computes n th fibonnaci number in

        O(n) Time Complexity

and     O(1) Space Complexity

It is so because the above function checks the two conditions for n == 0 and n == 1 in O(1) time.

The for loop runs for n - 2 times.

Hence, Time Complexity = O(1) + O(1) + O(n - 2)

                       = O(n - 2) (as O(1) is negligible)

                       = O(n) (as (n - 2) = n if n is very large)

Add a comment
Know the answer?
Add Answer to:
1. (10 points) Write an efficient iterative (i.e., loop-based) function Fibonnaci(n) that returns the nth Fibonnaci...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Subject: Algorithm solve only part 4 and 5 please. need urgent. 1 Part I Mathematical Tools and Definitions- 20 points, 4 points each 1. Compare f(n) 4n log n + n and g(n)-n-n. Is f E Ω(g),fe 0(g)...

    Subject: Algorithm solve only part 4 and 5 please. need urgent. 1 Part I Mathematical Tools and Definitions- 20 points, 4 points each 1. Compare f(n) 4n log n + n and g(n)-n-n. Is f E Ω(g),fe 0(g), or f E (9)? Prove your answer. 2. Draw the first 3 levels of a recursion tree for the recurrence T(n) 4T(+ n. How many levels does it have? Find a summation for the running time. (Extra Credit: Solve it) 3. Use...

  • Consider the function FINDDUPLICATES(A[0. 1) that returns a list of all duplicate items in the unsorted integer array A...

    Consider the function FINDDUPLICATES(A[0. 1) that returns a list of all duplicate items in the unsorted integer array A of size n. For example, given the array |3, 2, 4, 3], the function should return the value 3. For the array 11 2,3 5,5,5,66,81, the function should return an array (or list) 5,6 In this task, you will develop two alternative solutions for the FINDDUPLICATES(Ao..n 1 func- tion. In your implementations, you may call any of the algorithms introduced in...

  • Please answer this in python pseudocode. It's an algorithm question. 1. [10 marks] Consider the function...

    Please answer this in python pseudocode. It's an algorithm question. 1. [10 marks] Consider the function SumKSmallest(A[0..n – 1), k) that returns the sum of the k smallest elements in an unsorted integer array A of size n. For example, given the array A=[6,-6,3,2,1,2,0,4,3,5] and k=3, the function should return -5. a. [3 marks) Write an algorithm in pseudocode for SumKSmallest using the brute force paradigm. Indicate and justify (within a few sentences) the time complexity of your algorithm. b....

  • 6. Consider the following basic problem. You're given an array A consisting of n integers A[1],...

    6. Consider the following basic problem. You're given an array A consisting of n integers A[1], A[2], , Aln]. You'd like to output a two-dimensional n-by-n array B in which B[i, j] (for i <j) contains the sum of array entries Ali] through Aj]-that is, the sum A[i] Ai 1]+ .. +Alj]. (The value of array entry B[i. Λ is left unspecified whenever i >j, so it doesn't matter what is output for these values.) Here's a simple algorithm to...

  • Need help with 1,2,3 thank you. 1. Order of growth (20 points) Order the following functions...

    Need help with 1,2,3 thank you. 1. Order of growth (20 points) Order the following functions according to their order of growth from the lowest to the highest. If you think that two functions are of the same order (Le f(n) E Θ(g(n))), put then in the same group. log(n!), n., log log n, logn, n log(n), n2 V, (1)!, 2", n!, 3", 21 2. Asymptotic Notation (20 points) For each pair of functions in the table below, deternme whether...

  • Question2 0/5 pts If exact running time of an algorithm is T(n)-5n3+ n2 + 3n -5...

    Question2 0/5 pts If exact running time of an algorithm is T(n)-5n3+ n2 + 3n -5 where n is the input size, then which of the following is true? T(n)- O(n) RCOECEQuestion 3 0/5 pts Which of the following is the correct ranking of the functions listed below: logn. n2 n2n, 2. 1500. nlogn, 5 Question 4 5/5 pts to search

  • 1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector...

    1) a) Write MATLAB function that accepts a positive integer parameter n and returns a vector containing the values of the integral (A) for n= 1,2,3,..., n. The function must use the relation (B) and the value of y(1). Your function must preallocate the array that it returns. Use for loop when writing your code. b) Write MATLAB script that uses your function to calculate the values of the integral (A) using the recurrence relation (B), y(n) for n=1,2,... 19...

  • Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the...

    Write a C program, containing the following functions. Function int recursive_fibonacci(int n) computes and returns the nth F(n) using recursive algorithm (i.e., recursive function call). Fibonacci numbers are defined by F(1)=F(2)=1, F(i) = F(i-1)+F(i-2), i=2,… . Function int iterative_fibonacci(int n) computes and returns the nth Fibonacci number F(n) using iterative algorithm (i.e., loop). The main function measures the memory usage and run time of iterative_fibonacci(40) and recursive_fibonacci(40), and does the comparison. To capture the execution time by millisecond, it needs...

  • I understand how it was simplified to n^(∈/(sqrt(logn))), but I'm trying to understand how to prove...

    I understand how it was simplified to n^(∈/(sqrt(logn))), but I'm trying to understand how to prove that logn grows faster for 0<∈<1. The derivative seems too complicated to prove this via Lhopital's Rule, so I tried using WolframAlpha to compare the two with logn as the numerator: http://www.wolframalpha.com/input/?i=limit+as+n+approaches+infinity+(logn)%2F(n%5E(0.5%2F(sqrt(logn))))&rawformassumption=%7B%22FunClash%22,+%22log%22%7D+-%3E+%7B%22Log10%22%7D However, this gives me a result of 0 for any value above 0, which would mean that n^(∈/(sqrt(logn))) grows at a faster rate, even when 0<∈<1. When I try to graph it,...

  • For each of the following problems write a recurrence relation describing the running time of eac...

    For each of the following problems write a recurrence relation describing the running time of each of the following algorithms and determine the asymptotic complexity of the function defined by the recurrence relation. Justify your solution using substitution and carefully computing lower and upper bounds for the sums. Simplify and express your answer as Θ(n k ) or Θ(n k (log n)) wherever possible. If the algorithm takes exponential time, then just give exponential lower bounds. 5. func5 (A,n) /*...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT