-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
Added a bit manipulation program to determine even or odd numbers #2959
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9027c94
Create check_even_odd.cpp
git5v 13d062d
Update check_even_odd.cpp
git5v 030c69c
Create factorial_top_down_dp.cpp
git5v 07c6989
Merge branch 'master' into master
realstealthninja 5f213b9
Delete dynamic_programming/factorial_top_down_dp.cpp
git5v fb5a1d4
Create factorial_top_down_dp.cpp
git5v f0b4d76
Delete dynamic_programming/factorial_top_down_dp.cpp
git5v 14cd706
Update bit_manipulation/check_even_odd.cpp
git5v d1c9bac
Update bit_manipulation/check_even_odd.cpp
git5v 623867e
Update bit_manipulation/check_even_odd.cpp
git5v 66f3e41
Update bit_manipulation/check_even_odd.cpp
git5v a3f753d
Update check_even_odd.cpp
git5v d015179
Update check_even_odd.cpp
git5v d1ccf58
Merge branch 'master' into master
realstealthninja 034a525
Merge branch 'master' into master
realstealthninja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,88 @@ | ||
/** | ||
* @file | ||
* @brief Implementation to [Check if a number is Even or Odd using Bitwise Operator] | ||
* (https://www.log2base2.com/c-examples/bitwise/odd-or-even-program-in-c-using-bitwise-operator.html) | ||
* | ||
* @details | ||
* Given an integer N, determine whether it is even or odd using bitwise manipulation. | ||
* The least significant bit (LSB) of a binary number determines its parity: | ||
* - If the LSB is 0, the number is even. | ||
* - If the LSB is 1, the number is odd. | ||
* | ||
* This can be checked efficiently using the bitwise AND operator (&) with 1. | ||
* - If (N & 1) == 0, N is even. | ||
* - If (N & 1) == 1, N is odd. | ||
* | ||
* Worst Case Time Complexity: O(1) | ||
* Space Complexity: O(1) | ||
* | ||
* @author [Vedant Mukhedkar](https://github.com/git5v) | ||
*/ | ||
|
||
#include <cassert> /// for assert | ||
#include <cstdint> | ||
#include <iostream> /// for IO operations | ||
#include <string> | ||
git5v marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @namespace bit_manipulation | ||
* @brief Bit manipulation algorithms | ||
*/ | ||
namespace bit_manipulation { | ||
/** | ||
* @namespace even_odd | ||
* @brief Functions for checking if a number is even or odd using bitwise operations | ||
*/ | ||
namespace even_odd { | ||
|
||
/** | ||
* @brief Checks if a number is even or odd using bitwise AND. | ||
* @param N The number to check. | ||
* @returns "Even" if N is even, "Odd" if N is odd. | ||
*/ | ||
std::string checkEvenOdd(std::int64_t N) { | ||
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
git5v marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return (N & 1) == 0 ? "Even" : "Odd"; | ||
realstealthninja marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
} // namespace even_odd | ||
} // namespace bit_manipulation | ||
|
||
/** | ||
* @brief Self-test implementations | ||
* @returns void | ||
*/ | ||
static void test() { | ||
using bit_manipulation::even_odd::checkEvenOdd; | ||
|
||
// Test Even numbers | ||
assert(checkEvenOdd(0) == "Even"); | ||
assert(checkEvenOdd(2) == "Even"); | ||
assert(checkEvenOdd(100) == "Even"); | ||
assert(checkEvenOdd(-4) == "Even"); | ||
assert(checkEvenOdd(-1000) == "Even"); | ||
|
||
// Test Odd numbers | ||
assert(checkEvenOdd(1) == "Odd"); | ||
assert(checkEvenOdd(3) == "Odd"); | ||
assert(checkEvenOdd(101) == "Odd"); | ||
assert(checkEvenOdd(-5) == "Odd"); | ||
assert(checkEvenOdd(-999) == "Odd"); | ||
|
||
std::cout << "All test cases successfully passed!" << std::endl; | ||
} | ||
|
||
/** | ||
* @brief Main function | ||
* @returns 0 on exit | ||
*/ | ||
int main() { | ||
test(); // run self-test implementations | ||
|
||
// Example usage: | ||
// std::int64_t num; | ||
// std::cout << "Enter a number: "; | ||
// std::cin >> num; | ||
// std::cout << num << " is " << bit_manipulation::even_odd::checkEvenOdd(num) << std::endl; | ||
|
||
git5v marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return 0; | ||
} |
This file contains hidden or 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,64 @@ | ||
/** | ||
* @file | ||
* @brief [Factorial calculation using recursion and memoization (dynamic programming)](https://en.wikipedia.org/wiki/Factorial) | ||
* @details | ||
* This program computes the factorial of a non-negative integer using recursion | ||
* with memoization (top-down dynamic programming). It stores intermediate results | ||
* to avoid redundant calculations for improved efficiency. | ||
* | ||
* Time Complexity: O(n) | ||
* Space Complexity: O(n) | ||
@author [Vedant Mukhedkar](https://github.com/git5v) | ||
*/ | ||
|
||
#include <iostream> | ||
#include <cassert> // For test cases | ||
|
||
/// Array to store computed factorials for memoization | ||
long long memo[1000] = {0}; | ||
|
||
/** | ||
* @brief Computes the factorial of a non-negative integer using recursion and memoization. | ||
* @param n The integer whose factorial is to be computed | ||
* @returns The factorial of n | ||
*/ | ||
long long fact_rec(int n) { | ||
if (n == 0) return 1; // Base case: 0! = 1 | ||
if (memo[n] != 0) return memo[n]; // Return already computed value | ||
memo[n] = n * fact_rec(n - 1); // Store and return the computed value | ||
return memo[n]; | ||
} | ||
|
||
/** | ||
* @brief Self-test implementations for the fact_rec function. | ||
* @returns void | ||
*/ | ||
void test_fact_rec() { | ||
// Test cases for factorial computation | ||
assert(fact_rec(0) == 1); | ||
assert(fact_rec(1) == 1); | ||
assert(fact_rec(5) == 120); | ||
assert(fact_rec(10) == 3628800); | ||
std::cout << "All test cases passed!\n"; | ||
} | ||
|
||
/** | ||
* @brief Main function to run test cases and interact with the user. | ||
* @returns 0 on program success | ||
*/ | ||
int main() { | ||
// Run test cases | ||
test_fact_rec(); | ||
|
||
// User interaction loop | ||
// int n; | ||
// std::cout << "Enter a non-negative integer to compute factorial (0 to exit): "; | ||
// std::cin >> n; | ||
// if (n < 0) { | ||
// std::cout << "Please enter a non-negative integer only.\n"; | ||
// return 0; | ||
// } | ||
// std::cout << "Factorial of " << n << " = " << fact_rec(n) << std::endl; | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.