1 / 4

CHAPTER 2

CHAPTER 2. Mathematics for Algorithms. Example 2.2.6 Factorial. This algorithm computes the factorial of n. factorial ( n ) { i = 1 fact = 1 while ( i < n ) { i = i + 1 fact = fact * i } return fact }.

Télécharger la présentation

CHAPTER 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CHAPTER 2 Mathematics for Algorithms

  2. Example 2.2.6 Factorial This algorithm computes the factorial of n. factorial(n) { i = 1 fact = 1 while (i < n) { i = i + 1 fact = fact * i } return fact }

  3. Example 2.3.1 Finding the Maximum Value in an Array Using a While Loop This algorithm finds the largest number in the array s[1], s[2], ... , s[n]. Input Parameter: s Output Parameters: None array_max_ver1(s) { large = s[1] i = 2 while (i ≤ s.last) { if (s[i] > large) // larger value found large = s[i] i = i + 1 } return large }

  4. Example 2.4.3 example(n) { if (n == 1) return for i = 1 to n x = x + 1 example(n/2) }

More Related