Showing posts with label bits. Show all posts
Showing posts with label bits. Show all posts

Friday, 20 May 2016

Challenge #2





  

Mr. Algorithm have an integer number, which he want to reverse by following steps:
  1. Convert the number into binary string.
  2. Reverse binary string.
  3. Convert the reversed binary string back to integer.
Can you help him write a function to do it ?

Example:
For x = 234, the output should be
ReverseBit(x) = 87.
23410 = 111010102 => 010101112 = 8710.
Input/Output
  • [input] integer x
    A non-negative integer.
  • [output] integer
    x reversed as described above.

    If you want, you can check my solution for only 1 euro! (check PayPal button below):

     

Monday, 9 May 2016

Greatest common divisor



         In mathematics, the greatest common divisor(gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder(amount left over after peforming a division).

Example:
gcd(8, 12) = 4

         The greatest common divisor is also known as the greatest common factor(gcf), highest common factor(hcf), greatest common measure(gcm) or highest common divisor.

         I will present you the algorithm for this below:

#include <iostream>
using namespace std;

int gcd(int a, int b) // greatest common divisor of two numbers using repeated falls
{
    while(a != b) // while this two numbers are different
    {
        if(a > b)
            a = a - b;
        else
            b = b - a;
    }   
    return a; // greatest common divisor will be saved in first number
}

int main()
{
    cout<<gcd(8, 12);
    cout<<endl;
    return 0;
}
There are 5 more methods to find gcd, more optimized. One of them complexity is best. Check bellow buttons(PayPal)!!!



Most optimized solution: