Slides - Recursion

Slides - Recursion - base case ¾ Then determine the...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Loops Loops – Iteration – Recursion 9 factorial N! = N*(N-1)! 9 prefix to postfix conversion 9 the Towers of Hanoi Recursive Factorial Algorithm recursiveFactorial ( n ) // calculate factorial of a number using recursion if ( n equals 0 ) return 1; else return ( n * recursiveFactorial ( n-1 ) ) end if
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Iterative Factorial Algorithm iterativeFactorial ( n ) // calculate the factorial of a number using a loop set i to 1 set factN to 1 loop ( i <= n ) set factN to factN * i increment i end loop return factN Recursive algorithm: ¾ First, determine the
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Background image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: base case. ¾ Then, determine the general case. ¾ Combine the base case and the general case into an algorithm. 3 GCD ( Greatest Common Division ) Algorithm gcd ( a, b ) // calculate Greatest Common Divisor GCD // of a and b if ( b equals 0 ) return a end if if ( a equals 0 ) return b end if return gcd (b, a mod b) 4-- Towers of Hanoi ?? (more complex) rules : 9 move only one disk at a time 9 a larger disk cannot be on the top of a smaller one 9 only one spare needle can be used...
View Full Document

{[ snackBarMessage ]}

Page1 / 4

Slides - Recursion - base case ¾ Then determine the...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online