diff --git a/src/algorithms/uncategorized/hanoi-tower/README.md b/src/algorithms/uncategorized/hanoi-tower/README.md index 581d3c19..816df8d3 100644 --- a/src/algorithms/uncategorized/hanoi-tower/README.md +++ b/src/algorithms/uncategorized/hanoi-tower/README.md @@ -21,7 +21,7 @@ Animation of an iterative algorithm solving 6-disk problem With `3` disks, the puzzle can be solved in `7` moves. The minimal number of moves required to solve a Tower of Hanoi puzzle -is `2n − 1`, where `n` is the number of disks. +is `2^n − 1`, where `n` is the number of disks. ## References diff --git a/src/algorithms/uncategorized/hanoi-tower/__test__/hanoiTower.test.js b/src/algorithms/uncategorized/hanoi-tower/__test__/hanoiTower.test.js index f6c9a788..c7ef66c4 100644 --- a/src/algorithms/uncategorized/hanoi-tower/__test__/hanoiTower.test.js +++ b/src/algorithms/uncategorized/hanoi-tower/__test__/hanoiTower.test.js @@ -3,10 +3,11 @@ import hanoiTower from '../hanoiTower'; describe('hanoiTower', () => { it('should solve tower of hanoi puzzle with 2 discs', () => { const moveCallbackMock = jest.fn(); + const numberOfDiscs = 2; - hanoiTower(2, moveCallbackMock); + hanoiTower(numberOfDiscs, moveCallbackMock); - expect(moveCallbackMock).toHaveBeenCalledTimes(3); + expect(moveCallbackMock).toHaveBeenCalledTimes((2 ** numberOfDiscs) - 1); expect(moveCallbackMock.mock.calls[0][0]).toBe(1); expect(moveCallbackMock.mock.calls[0][1]).toEqual([1, 2]); @@ -23,17 +24,19 @@ describe('hanoiTower', () => { it('should solve tower of hanoi puzzle with 3 discs', () => { const moveCallbackMock = jest.fn(); + const numberOfDiscs = 3; - hanoiTower(3, moveCallbackMock); + hanoiTower(numberOfDiscs, moveCallbackMock); - expect(moveCallbackMock).toHaveBeenCalledTimes(7); + expect(moveCallbackMock).toHaveBeenCalledTimes((2 ** numberOfDiscs) - 1); }); it('should solve tower of hanoi puzzle with 6 discs', () => { const moveCallbackMock = jest.fn(); + const numberOfDiscs = 6; - hanoiTower(6, moveCallbackMock); + hanoiTower(numberOfDiscs, moveCallbackMock); - expect(moveCallbackMock).toHaveBeenCalledTimes(63); + expect(moveCallbackMock).toHaveBeenCalledTimes((2 ** numberOfDiscs) - 1); }); });