E7_Su2010_Solutions5_4-5

E7_Su2010_Solutions5_4-5 - 7/19/2010 Lab 5, Partial...

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

View Full Document Right Arrow Icon
Lab 5, Partial Solutions Contents Problem 4 (Towers of Hanoi) (a) (b) (c) (d) Problem 5 (Fibonacci Sequence) (a) (b) (c) Problem 4 (Towers of Hanoi) (a) According to Figure 12, It takes _S(N)_ steps to uncover disk _N+1_. It takes just one step to move disk _N+1_ to its final position. It takes another _S(N)_ steps to put the remaining _N_ disks back on top of disk _N+1_. Therefore, _S(N+1) = S(N) + 1 + S(N) = 2*S(N) + 1_. (b) type towersteps function S = towersteps(N) if N == 1 S = 1; else % Use the recurrence relation from part (a) S = 2*towersteps(N-1) + 1; end towersteps(1) ans = 1 towersteps(2) ans = 3 7/19/2010 Lab 5, Partial Solutions C:/…/lab05soln.html 1/5
Background image of page 1

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

View Full DocumentRight Arrow Icon
towersteps(3) ans = 7 towersteps(10) ans = 1023 towersteps(64) ans = 1.8447e+019 (c) numDisks = towersteps(64); disksPerSecond = 1; secondsPerMin = 60; secondsPerHour = 60*secondsPerMin; secondsPerDay = 24*secondsPerHour; secondsPerYear = 365*secondsPerDay; numYears = numDisks/disksPerSecond/secondsPerYear numYears = 5.8494e+011 (d) type towers function towers(n,initialTower,finalTower) if n == 1 % If we only have one disk to move, that disk is always 'Disk 1'. message = ['Move Disk 1 from Tower ', num2str(initialTower), .
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/14/2010 for the course E 7 taught by Professor Patzek during the Summer '08 term at University of California, Berkeley.

Page1 / 5

E7_Su2010_Solutions5_4-5 - 7/19/2010 Lab 5, Partial...

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

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