Homework – Chapter 1 作業解答. Problem 1 Given the Fibonacci number as 1 2 3 5 8 13 21 34… where the next Fibonacci number will be the sum of its previous.

Slides:



Advertisements
Similar presentations
Chapter 20 Recursion.
Advertisements

Recursion.
Operations with Functions Objective: Perform operations with functions and find the composition of two functions.
Excursions in Modern Mathematics Sixth Edition
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Chapter 5: Control Structures II (Repetition)
The search for good information To help you guide investments.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Entry task 1) What can be concluded from the following pattern? = 15 = 3 × = 20 = 4 × = 25 =
Recursion.
Computer Science Recursion Yuting Zhang Allegheny College, 04/24/06.
Problem-Solving Strategy: Look for a pattern
By: Clara Ditto, Joe Gaone, Christina Ward Fibonacci Sequence 0, 1 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 … = 10, = 20, 1,
For(int i = 1; i
Tree Recursion Traditional Approach. Tree Recursion Consider the Fibonacci Number Sequence: Time: , 1, 1, 2, 3, 5, 8, 13, 21,... /
Chapter 4 Methods F Introducing Methods –Benefits of methods, Declaring Methods, and Calling Methods F Passing Parameters –Pass by Value F Overloading.
Lesson 10.2: Arithmetic Sequences & Series
Φ.  The golden ratio is a ratio that states if the ratio of the sum of the quantities to the larger quantity is equal to the ratio of the larger quantity.
Starting Out with Java: From Control Structures through Objects
KOLAM DESIGNS BASED ON FIBONACCI NUMBERS S. Naranan 30 January 2008 Copyright: Prof. S. Naranan, Chennai, India.
Fibonacci Born in: Pisa in 1175 Nickname: Fibonacci Real name: Leonardo Pisano or Leonardo of Pisa.
Linear vs Binary Search COP What is recursion? // Pre-conditions: exponent is >= to 0 // Post-conditions: returns base exponent int Power(int base,
MAT 4 – Kompleks Funktionsteori MATEMATIK 4 INDUKTION OG REKURSION MM 1.4 MM 1.4: Induktion og Rekursion Topics: Mathematical induction Example of Towers.
Chapter 7 Sorting Part II. 7.3 QUICK SORT Example left right pivot i j 5 > pivot and should go to the other side. 2 < pivot and should go to.
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
C1 Chapter 6 Arithmetic Series Dr J Frost Last modified: 7 th October 2013.
Case study 1: Calculate the approximation of Pi
Ics202 Data Structures. import java.util.Scanner; public class Name { static int Algorithm Name (int n) { … The Algorithm … } public static void main.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 9/18/2009.
Number Patterns. What are they? Number patterns are a non-ending list of numbers that follow some sort of pattern from on number to the next.
7.5 Use Recursive Rules with Sequences and Functions
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
Honors Precalculus: Do Now Find the nth term of the sequence whose first several terms are given. Find the first 4 terms of the given recursively defined.
12-1 Arithmetic Sequences and Series. Sequence- A function whose domain is a set of natural numbers Arithmetic sequences: a sequences in which the terms.
THE BEST CLASS EVER…ERRR…. PRE-CALCULUS Chapter 13 Final Exam Review.
Introduction Session 4. Generate the first N Fibonacci #s 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,... The nth Fibonacci number is the sum of the.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 19 Recursion.
1 Object-Oriented Programming Using C++ CLASS 2. 2 Linear Recursion Summing the Elements of an Array Recursively Algorithm LinearSum(A, n): Input: An.
RECURSION.
What is happening here? 1, 1, 2, 3, 5, 8 What is after 8? What is the 10 th number?
A Recursive View of Functions: Lesson 45. LESSON OBJECTIVE: Recognize and use recursion formula. DEFINITION: Recursive formula: Each term is formulated.
Lecture#16 Discrete Mathematics. Recursion Now, 1 is an odd positive integer by the definition base. With k = 1, = 3, so 3 is an odd positive integer.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Recursion Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 7 © 2002 Addison Wesley.
Unit 5 – Series, Sequences, and Limits Section 5.2 – Recursive Definitions Calculator Required.
Chapter 5 – Functions II Outline Recursion Examples Using Recursion: The Fibonacci Series.
Sequences & Series Section 13.1 & Sequences A sequence is an ordered list of numbers, called terms. The terms are often arranged in a pattern.
Week 4 Warm Up ) Geometric or Arithmetic? -2, 4, -8, 16, -32, 64,…
Leonardo Pisano Fibonacci
Lesson 11-1 Sequences. Vocabulary Sequence – a list of numbers written in a definite order { a 1, a 2, a 3, a n-1, a n } Fibonacci sequence – a recursively.
13.2: Arithmetic and Geometric Sequences
Infinite Geometric Series Recursion & Special Sequences Definitions & Equations Writing & Solving Geometric Series Practice Problems.
Essential Questions Introduction to Sequences
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
9.3 Geometric Sequences and Series. Common Ratio In the sequence 2, 10, 50, 250, 1250, ….. Find the common ratio.
R E C U R S I O N Legaspi, Ma. ArleneProf. Carmela Francisco MITSAT 8:00 am to 4pm.
{1, 1, 2, 3, 5, 8, 13, 21,...} What is this? Fibonacci Sequence.
Do Now Solve the inequality and come up with a real world scenario that fits the solution. Also Pick up a textbook.
8.1 – Sequences and Series. Sequences Infinite sequence = a function whose domain is the set of positive integers a 1, a 2, …, a n are the terms of the.
Sequences and Series 9.1.
常微分方程作業 Ch 應數大二 洪美玲
Intro to Recursion.
Objective: To find the area of a trapezoid.
Quick Graphs Using Slope-Intercept Form
A number is divisible by 2 (is even) if it has a ones digit of 0, 2, 4, 6, or 8 (that is, it has an even ones digit). Examples: A number is divisible by.
Objective: Learn to test for divisibility.
Warm Up Write the first 4 terms of each sequence:
Presentation transcript:

Homework – Chapter 1 作業解答

Problem 1 Given the Fibonacci number as … where the next Fibonacci number will be the sum of its previous two Fibonacci number. –Example: The number of 8 is the sum of 3 and 5. The next number will be 55 since it is the sum of 21 and 34.

Problem 1 1.Write a recursive algorithm to generate the Fibonacci number N. int Fibonacci (int N) { if (N <= 2) return N; return Fibonacci(N-1) + Fibonacci(N-2); }

Problem 1 2.Write a non-recursive algorithm using loop structure to generate the Fibonacci number N. int Fibonacci2 (int N) { if (N <= 2) return N; Let N1 = 1, N2 = 2, Total = 0; for (i = 3 to N) { Total = N1 + N2; N1 = N2; N2 = Total; } return Total; }

Problem 1 3.Discuss the execution efficiency of these two programs. –The process of the recursive Fibonacci() can be illustrated by the following tree structure. –Space complexity of Fibonacci(): At most N-1 stack frames (the depth of the tree) are pushed into function call stack when Fibonacci() is called. Therefore, the complexity is O(N). –Time complexity of Fibonacci(): Therefore, the time complexity depends on how many nodes the tree has, which is O(2 N ).

–Space complexity of Fibonacci2(): Only a fix number of automatic variables are required. Therefore, the complexity is O(1). –Time complexity of Fibonacci2(): The for-loop runs in N-2 times. Its body perform addition in O(1) for each iteration. Therefore, the total computing time is O(N). Conclusion: –Fibonacci2() is more efficient than Fibonacci(). Its space complexity and time complexity are relatively lower.

Problem 2 Ordering by asymptotic growth rates: (A)log(n!) (B)4 logn (C)(n-1)! (D)n . 2 n (E)(logn) logn. –Please write computation!

Solution (A)log(n!) log(n!) < log(n n ) = O(n logn) (B)4 logn Let S = 4 logn logS = log4 logn = log2 2logn = 2logn = logn 2 ∴ S = n 2 = O(n 2 ) (C)(n-1)! = O(n!) (D)n . 2 n < 2 n . 2 n = 2 2n = O(2 n ) ∴ (A) < (B) < (D) < (C)

Solution (E)(logn) logn Let S = (logn) logn log S = (logn)(loglogn) = log(n loglogn ) S = n loglogn < n logn < 2 n 2 n is growing faster than n logn. ∴ (A) < (B) < (E) < (D) < (C) nn logn 2n2n ………