Software Wizard – Omniceps http://www.omniceps.com Advanced IT solutions Thu, 22 Mar 2018 08:54:09 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.6 http://www.omniceps.com/wp-content/uploads/2017/07/cropped-final_o_transparent-32x32.png Software Wizard – Omniceps http://www.omniceps.com 32 32 Solution of Tower Of Hanoi Problem with PHP http://www.omniceps.com/tower-of-hanoi-php-solution/ http://www.omniceps.com/tower-of-hanoi-php-solution/#respond Sun, 10 Sep 2017 11:44:25 +0000 http://www.omniceps.com/?p=3336 The Tower Of Hanoi puzzle is a problem of Recursion which was invented by the French mathematician Edouard Lucas in 1883. Today we will look at its solution algorithm and also solution using PHP. Here is what Wikipedia says about … Read More

The post Solution of Tower Of Hanoi Problem with PHP appeared first on Omniceps.

]]>
The Tower Of Hanoi puzzle is a problem of Recursion which was invented by the French mathematician Edouard Lucas in 1883. Today we will look at its solution algorithm and also solution using PHP.

Here is what Wikipedia says about Tower of Hanoi. Read Here ->
This is a mathematical puzzle, which consists of a set of three towers (pegs) and n number of disks, with each disk of different size. The puzzle starts with all disks present in a stack in descending order from top to bottom i.e. smallest disc at the top. Here how the problem looks with n=3 disks.

3 disc solution of tower of hanoi omniceps

There are certain rules to solve this problem:

1. You can move only one disk at a time.
2. Only the disk present at the top can be removed (FIFO method).
3. You cannot put larger disk on top of smaller disk. Example: Considering the above image,you cannot put number 3 disk on top of number 2 disk.

With n=3, this problem can be solved in total 7 moves.  The minimal number of moves required to solve a Tower of Hanoi puzzle is 2 n  − 1, where n is the number of disks.

Now we will look at the Algorithm to solve this problem:

In order to write the algorithm we mark three towers (pegs) as P1, P2, P3 and number of disks as n and our task is to move all the disks present at P1 to P2.

First Condition: If there is only one disk i.e. if n=1, then it can be simply moved from P1 to P2.

Second Condition: If two disks are available i.e. if n=2, then:

1. Move the smaller (top) disk to P3.
2. Then, move the larger (bottom) disk to P2.
3. Finally, move the smaller disk from P3 to P2.

Now, The Algorithm for n number of disks can be designed as:

START
Procedure Hanoi(n,P1,P2,P3)

IF n == 1, THEN
move disk from P1 to P2
ELSE
Hanoi(n - 1, P1, P3, P2)
move disk from P1 to P2
Hanoi(n - 1, P3, P2, P1)
END IF

END Procedure
STOP

 

PHP code for Tower of Hanoi Problem:

function move($n,$P1,$P2,$P3) {
    if ($n === 1) {
       print("Move disk from pole $P1 to pole $P2");
    } else {
       move($n-1,$P1,$P3,$P2);
       move(1,$P1,$P2,$P3);
       move($n-1,$P3,$P2,$P1);
    }

}

Check our more about our quiz section here->.

The post Solution of Tower Of Hanoi Problem with PHP appeared first on Omniceps.

]]>
http://www.omniceps.com/tower-of-hanoi-php-solution/feed/ 0
Problem Of Binary Gap http://www.omniceps.com/problem-binary-gap/ http://www.omniceps.com/problem-binary-gap/#respond Wed, 30 Aug 2017 21:39:20 +0000 http://www.omniceps.com/?p=3287 The Binary Gap is the number of Consecutive zeros in the binary representation of a decimal number surrounded by 1’s at both ends. The Problem of Binary Gap corresponds to finding the longest Binary Gap in a given Decimal number. … Read More

The post Problem Of Binary Gap appeared first on Omniceps.

]]>
The Binary Gap is the number of Consecutive zeros in the binary representation of a decimal number surrounded by 1’s at both ends. The Problem of Binary Gap corresponds to finding the longest Binary Gap in a given Decimal number. In programming terms this decimal number is an integer.

For example consider an integer 9 which can be represented as 1001 in binary digits. So here the Binary Gap of 9 will be said to be 2 as there are two consecutive zeros which are surrounded by 1’s at both ends. Similarly the number 41 can be represented in Binary as 101001, so it has two binary gaps but the bigger gap is of length 2. So the binary gap of 41 will be 2.

Another example is decimal number 4 which can be represented as 100 in binary. Although it has 2 consecutive zeros but they are not surrounded by 1’s. Due to this the Binary Gap of 4 is 0 or none.

So the problem is to write a function:

int binaryGap(int N)

such that when an integer N is passed to it the function returns the Binary Gap in its decimal representation. The function should return 0 in case no binary gap is present.

For example, given N = 41 the function should return 2.

Assumptions are listed Below:

  1. N is a positive integer with higher limit 2,147,483,647.

Expected complexities:

  1. Worst-case time complexity should be O(log(N));
  2. Worst-case space complexity is O(1).
Quiz Solution

Quiz Solution

Write the name of your programming language
Write your solution in your preferred programming language here
Please provide your email address so we can contact you in case you are winner of the quiz

The post Problem Of Binary Gap appeared first on Omniceps.

]]>
http://www.omniceps.com/problem-binary-gap/feed/ 0