Give an algorithm that gets a positive integer m as input an

Give an algorithm that gets a positive integer m as input and outputs its binary representation. Determine that worst-case time and best-case time complexities of your algorithm. Again, note that the time complexities must be expressed in terms of the size of the input.

Solution

binary(m){
   if(m<=1)
   {
       print(0)
   }
   t = m%2;
   binary(m/2);
   print(t);
}

The time taken for this algorithm is O(log(m)). In each execution of the function we are dividing the number by 2, so the function executes log(m) times.

 Give an algorithm that gets a positive integer m as input and outputs its binary representation. Determine that worst-case time and best-case time complexities

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site