-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Day-31: Math & Bit manipulation problems
- Loading branch information
Showing
3 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Given two numbers A and B, count number of flips required to convert number A to B | ||
* Approach : Take XOR of A and B, the result will contain set bits only at positions | ||
* where A differed B. Therefore number of set bits in the result of A ^ B is the number | ||
* of flips required to convert A to B | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
int countSetBits( int x ) | ||
{ | ||
int count = 0; | ||
while( x ) { | ||
++count; | ||
x = x & (x - 1); | ||
} | ||
return count; | ||
} | ||
|
||
int countBitFlips( int a, int b ) | ||
{ | ||
return countSetBits(a ^ b); | ||
} | ||
|
||
int main() | ||
{ | ||
int x, y; | ||
std::cout << "Enter number 1 :"; | ||
std::cin >> x; | ||
std::cout << "Enter number 2 :"; | ||
std::cin >> y; | ||
std::cout << "Bit flips required to convert " << x | ||
<< " to " << y << " is " << countBitFlips(x, y) << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Given two numbers, determine the greatest common divisior of the two numbers. | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
|
||
int gcd1( int a, int b ) { | ||
while( b > 0 ) { | ||
int temp = a % b; | ||
a = b; | ||
b = temp; | ||
} | ||
return a; | ||
} | ||
|
||
int gcd2( int a, int b ) { | ||
if ( b == 0 ) return a; | ||
if ( a == 0 ) return b; | ||
return gcd2(b, a % b); | ||
} | ||
|
||
int gcd3( int a, int b ) { | ||
if ( a == 0 ) return b; | ||
if ( b == 0 ) return a; | ||
while ( a != b ) { | ||
if ( a > b ) { | ||
a = a-b; | ||
} else { | ||
b = b-a; | ||
} | ||
} | ||
return a; | ||
} | ||
|
||
int gcd4 ( int a, int b ) { | ||
if ( a == 0 ) return b; | ||
if ( b == 0 ) return a; | ||
if ( a == b ) return a; | ||
if ( a > b ) return gcd4(b, a-b); | ||
else return gcd4(a, b-a); | ||
} | ||
|
||
int main() | ||
{ | ||
int a, b; | ||
std::cout << "Enter number 1 : "; | ||
std::cin >> a; | ||
std::cout << "Enter number 2 : "; | ||
std::cin >> b; | ||
std::cout << "GCD of " << a << " and " | ||
<< b << " is " << gcd1(a,b) << std::endl; | ||
std::cout << "GCD of " << a << " and " | ||
<< b << " is " << gcd2(a,b) << std::endl; | ||
std::cout << "GCD of " << a << " and " | ||
<< b << " is " << gcd3(a,b) << std::endl; | ||
std::cout << "GCD of " << a << " and " | ||
<< b << " is " << gcd4(a,b) << std::endl; | ||
return 0; | ||
} |