16 views

Uploaded by Michael Reid

- Revised Lesson Plan
- Analysis of Algorithms I
- Infinite Math Syllabus
- Holtshausen_paper
- CalTech Version II
- sequence activities for prezi
- Assignment 1 Solutions
- L01 AlgoAnalysis
- Paleo Chart - Arogyam Nalvazvu
- Homework7 Solutions
- Bi Go
- an_introduction_to_group_theory.pdf
- Furrows in the Ale Tho Sphere 4
- math-guide-third.pdf
- Special Symbols and Operation Level 5-Session 1 (SC) (1)
- Algebra 1 > Notes > YORKCOUNTY FINAL > Unit 3 > Lesson_8 - Solving Literal Equations
- talgoPolyn1
- Order of Operations
- Algorithm Question Set
- 4-Grd6 TX u1 l4 Unay Revision

You are on page 1of 3

2.1 In the test case b = 2, n = 11, the simple power algorithm performs 11 multiplications, while the smart power algorithm performs 7 multiplications. Algorithm 1.1 has time complexity O(1). The matrixAdd method performs n2 additions. Its time complexity is O(n2). The matrixMult method performs n3 additions and n3 multiplications. Its time complexity is O(n3). 2.5 To analyze Algorithm 2.16, count the number of characters required to render i to base r. If i is positive, the number of characters is logr i + 1. If i is negative, the number of characters is logr(abs(i)) + 2 (the extra character being ). The time complexity is O(log(abs(i))). To print a given integer i to base r: 1. Set s to the empty string . 2. Set p to the absolute value of i. 3. Repeat the following until p = 0: 3.1. Let d be the digit corresponding to (p modulo r). 3.2. Prepend d to s. 3.3. Divide p by r. 4. If i < 0, prepend to s. 5. Print s. 6. Terminate. This algorithms time complexity is O(log(abs(i))). 2.7 To find the GCD of positive integers m and n (recursive version): 1. Let p be the greater and q the lesser of m and n. 2. If p is a multiple of q: 2.1. Terminate with answer q. 3. If p is not a multiple of q: 3.1. Let g be the GCD of q and (p modulo q). 3.2. Terminate with answer g. 2.8 Algorithm 2.21 performs n multiplications. Its time complexity is O(n). Method to calculate the factorial of n (recursive version):

static int factorial (int n) { if (n == 0) return 1; else return n * factorial(n-1); }

2.2 2.4

2.6

To calculate the factorial of n (non-recursive version): 1. Set f to 1. 2. For i = 1, , n, repeat: 2.1. Multiply f by i. 3. Terminate with answer f. Method to calculate the factorial of n (non-recursive version):

2-1

static int factorial (int n) { int f = 1; for (int i = 1; i <= n; i++) f *= i; return f; }

2.9

Let the Fibonacci function be fib(n). Tabulate the first few Fibonacci numbers, and the ratios of consecutive numbers:

n fib(n) fib(n)/fib(n1) 0 1 1 1 1.00 2 2 2.00 3 3 1.50 4 5 1.67 5 8 1.60 6 13 1.63 7 21 1.62 8 34 1.62

Thus we can see that fib(n) cbn, where b 1.62 and c 0.72. Suppose that Algorithm 2.22 performs adds(n) additions. It is easy to see that adds(n) = fib(n) 1 cbn 1. The algorithms time complexity is therefore O(bn). To calculate the Fibonacci number of n (non-recursive version): 1. If n 1: 1.1. Terminate with answer 1. 2. If n > 1: 2.1. Set oldfib to 1, and set fib to 1. 2.2. For i = 2, , n, repeat: 2.2.1. Set oldfib and fib to fib and oldfib+fib, respectively. 2.3. Terminate with answer fib. Method to calculate the Fibonacci number of n (recursive version):

static int fibonacci (int n) { if (n <= 1) return 1; else return fibonacci(n-1) + fibonacci(n-2); }

static int fibonacci (int n) { if (n <= 1) return 1; else { int oldfib = 1, fib = 1; for (int i = 2; i <= n; i++) { int newfib = oldfib + fib; oldfib = fib; fib = newfib; } return fib; }

2.10

Outline of program:

2-2

static void moveTower (int n, int source, int dest) { if (n == 1) moveDisk(source, dest); else { int spare = 6 - source - dest; moveTower(n-1, source, spare); moveDisk(source, dest); moveTower(n-1, spare, dest); } } static void moveDisk (int source, int dest) { System.out.println("Move disk from " + source + " to " + dest); }

To make the program count the moves, modify moveTower to return the required number of moves, as follows:

static int moveTower (int n, int source, int dest) { if (n == 1) { moveDisk(source, dest); return 1; } else { int spare = 6 - source - dest; int moves1 = moveTower(n-1, source, spare); moveDisk(source, dest); int moves2 = moveTower(n-1, spare, dest); return moves1 + 1 + moves2; } }

2-3

- Revised Lesson PlanUploaded byKaitlyn Leatherman
- Analysis of Algorithms IUploaded byFahri Firdausillah
- Infinite Math SyllabusUploaded byptbestari
- Holtshausen_paperUploaded bybpd21
- CalTech Version IIUploaded byelyzai
- sequence activities for preziUploaded byapi-242208852
- Assignment 1 SolutionsUploaded byJamesRuth
- L01 AlgoAnalysisUploaded byEnmusk
- Paleo Chart - Arogyam NalvazvuUploaded byamzatha8743
- Homework7 SolutionsUploaded bycrazwomanyo
- Bi GoUploaded byFarizParahitaRoestam
- an_introduction_to_group_theory.pdfUploaded byMiguel Naupay
- Furrows in the Ale Tho Sphere 4Uploaded byChun-hsiung Chen
- math-guide-third.pdfUploaded byraberoan
- Special Symbols and Operation Level 5-Session 1 (SC) (1)Uploaded byMl Phil
- Algebra 1 > Notes > YORKCOUNTY FINAL > Unit 3 > Lesson_8 - Solving Literal EquationsUploaded byapi-3776731
- talgoPolyn1Uploaded byharshit
- Order of OperationsUploaded bymaszam3
- Algorithm Question SetUploaded bylokenders801
- 4-Grd6 TX u1 l4 Unay RevisionUploaded byAngelo Aniag Unay
- data structures question bankUploaded byAnand Kumar
- 00001740Uploaded bySaShahin
- 7em_mat.pdfUploaded bynasreen
- Math Multiplication Table FactsUploaded byNatasha Oliver
- AlgorithmAnalysis-BookChapter (1)Uploaded byDivya Varshaa
- Damath NotesUploaded byRene N. Martinez
- INMO.pdfUploaded byAjjx
- Analysis of AlgorithmsUploaded bytezukafuji
- DSUploaded byYogesh Verma
- Introduction to Exponents Part 1 10-7Uploaded byDet212

- Math Questions and AnswersUploaded byChrisEdwardAnatalio
- Chapter 11Uploaded bysaied_h2004
- Wavelet Methods for Time Series AnalysisUploaded byAmarjargal Norov
- Dasmani's QuizUploaded byFrederick Adjepong
- Andreas Brandhuber, Bill Spence and Gabriele Travaglini-Tree-Level FormalismUploaded bySteam29
- 3 29 17 notes for combining and composition of functionsUploaded byapi-325344728
- The Dynamics of Human Social Behavior: Qualitative Mathematical Description of Human InteractionsUploaded byPavel Barseghyan
- Conic Sections FormulasUploaded byMulti talented India
- Harmonic NotesUploaded byAfeef Abu Bakar
- M1A1probs1Uploaded bySean Le
- Convergence, Continuity and Topology-GarlingUploaded byKhmer Cham
- Math54 2ndexam exercisesUploaded byAbby Dalag
- finnigan 2000Uploaded byMark Rudnicki
- abstract-algebra-ch13-dummit-foote2.pdfUploaded byThiago Nobre
- Poisson Process GenerationUploaded byMadhu Babu Sikha
- Stability TheoryUploaded byAaron Crighton
- Linear ProgramUploaded bySoumya Bsoumya
- VectorUploaded byAnkit Gupta
- hw5_solUploaded byDylan Ler
- Introduction to Actuarial Science - ElementaryUploaded byPolelar
- Math Exam ReviewUploaded bySim
- Acoustic Wave EquationUploaded byriyadi
- 12_calculusii_wksht10.pdfUploaded byKingom 885
- Problem Set 1 SolutionsUploaded byahmbash
- CHERN, MOSER - Real Hypersufarce in Complex ManifoldsUploaded byLuiz Gustavo Carneiro
- KrigingUploaded byBuiNgocHieu
- HSS User GuideUploaded byrhutchcroft
- nDSP3Uploaded byelamaran_vlsi
- Propiedades Extremas de Valores Propios TropicalesUploaded byWendy Villogas Espinoza
- Relativistic Killingbeck Energy States Under an External Magnetic FieldsUploaded byYash