Docsity

Exercises for seniors, Exercises of Law

Toggle sidebar
bg1
Programming Assignment โ€” Tower of Hanoi (4 Discs)
Name: Omar Ehab Abdelmoniem Mahmoud El-Banna
ID: 11172
Question: Write the 15 steps of a 4-disc Tower of Hanoi
Algorithm Idea:
To move n disks from Source (A) to Destination (C) using Auxiliary (B):
1. Move nโ€“1 disks from A โ†’ B
2. Move the nth disk from A โ†’ C
3. Move nโ€“1 disks from B โ†’ C
Solution for 4 Discs (A = Source, B = Auxiliary, C = Destination):
โ€ข Total Moves = 2โด โ€“ 1 = 15 moves
Conclusion: Using recursion, the Tower of
Hanoi problem with 4 disks requires 15
moves to transfer all disks from Source to
Destination following the rules.
Step
Move Description
1
Move disk 1 from A โ†’ B
2
Move disk 2 from A โ†’ C
3
Move disk 1 from B โ†’ C
4
Move disk 3 from A โ†’ B
5
Move disk 1 from C โ†’ A
6
Move disk 2 from C โ†’ B
7
Move disk 1 from A โ†’ B
8
Move disk 4 from A โ†’ C
9
Move disk 1 from B โ†’ C
10
Move disk 2 from B โ†’ A
11
Move disk 1 from C โ†’ A
12
Move disk 3 from B โ†’ C
13
Move disk 1 from A โ†’ B
14
Move disk 2 from A โ†’ C
15
Move disk 1 from A โ†’ C

Partial preview of the text

Download Exercises for seniors and more Exercises Law in PDF only on Docsity!

Programming Assignment โ€” Tower of Hanoi (4 Discs)

Name: Omar Ehab Abdelmoniem Mahmoud El-Banna ID: 11172 Question: Write the 15 steps of a 4-disc Tower of Hanoi

Algorithm Idea:

To move n disks from Source (A) to Destination (C) using Auxiliary (B):

  1. Move nโ€“1 disks from A โ†’ B
  2. Move the nth disk from A โ†’ C
  3. Move nโ€“1 disks from B โ†’ C

Solution for 4 Discs (A = Source, B = Auxiliary, C = Destination):

  • Total Moves = 2โด โ€“ 1 = 15 moves Conclusion : Using recursion, the Tower of Hanoi problem with 4 disks requires 15 moves to transfer all disks from Source to Destination following the rules. Step Move Description 1 Move disk 1 from A โ†’ B 2 Move disk 2 from A โ†’ C 3 Move disk 1 from B โ†’ C 4 Move disk 3 from A โ†’ B 5 Move disk 1 from C โ†’ A 6 Move disk 2 from C โ†’ B 7 Move disk 1 from A โ†’ B 8 Move disk 4 from A โ†’ C 9 Move disk 1 from B โ†’ C 10 Move disk 2 from B โ†’ A 11 Move disk 1 from C โ†’ A 12 Move disk 3 from B โ†’ C 13 Move disk 1 from A โ†’ B 14 Move disk 2 from A โ†’ C 15 Move disk 1 from A โ†’ C