Skip to content

Commit 16ad0a7

Browse files
committed
Add code snippets for math-and-numbers, searching, sorting, bit manipulation
1 parent f446d74 commit 16ad0a7

21 files changed

+426
-29
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Check if a Number is Power of Two
3+
description: Checks if a given number is a power of two using bitwise operations.
4+
tags: bit-manipulation, power-of-two
5+
author: ashukr07
6+
---
7+
8+
```cpp
9+
bool is_power_of_two(int n) {
10+
return n > 0 && (n & (n - 1)) == 0;
11+
}
12+
13+
// Usage:
14+
is_power_of_two(16); // Returns: true
15+
is_power_of_two(18); // Returns: false
16+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Find Non-Repeating Number
3+
description: Finds the number that appears only once in an array where every other number appears twice.
4+
tags: bit-manipulation, xor
5+
author: ashukr07
6+
---
7+
8+
```cpp
9+
int find_non_repeating(const std::vector<int>& nums) {
10+
int result = 0;
11+
for (int num : nums) {
12+
result ^= num;
13+
}
14+
return result;
15+
}
16+
17+
// Usage:
18+
std::vector<int> nums = {4, 1, 2, 1, 2};
19+
find_non_repeating(nums); // Returns: 4
20+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Reverse Bits
3+
description: Reverses the bits of a given integer.
4+
tags: bit-manipulation, reverse-bits
5+
author: ashukr07
6+
---
7+
8+
```cpp
9+
unsigned int reverse_bits(unsigned int n) {
10+
unsigned int result = 0;
11+
for (int i = 0; i < 32; ++i) {
12+
result <<= 1;
13+
result |= n & 1;
14+
n >>= 1;
15+
}
16+
return result;
17+
}
18+
19+
// Usage:
20+
reverse_bits(43261596); // Returns: 964176192 (Binary: 00000010100101000001111010011100 -> 00111001011110000010100101000000)
21+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Count Set Bits
3+
description: Counts the number of 1 bits in the binary representation of a number.
4+
tags: bit-manipulation, set-bits
5+
author: ashukr07
6+
---
7+
8+
```cpp
9+
int count_set_bits(int n) {
10+
int count = 0;
11+
while (n) {
12+
count += n & 1;
13+
n >>= 1;
14+
}
15+
return count;
16+
}
17+
18+
// Usage:
19+
count_set_bits(13); // Returns: 3 (Binary: 1101)
20+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: XOR of Range
3+
description: Finds XOR of all numbers from 1 to n using properties of XOR.
4+
tags: bit-manipulation, xor
5+
author: ashukr07
6+
---
7+
8+
```cpp
9+
int xor_upto_n(int n) {
10+
if (n % 4 == 0) return n;
11+
if (n % 4 == 1) return 1;
12+
if (n % 4 == 2) return n + 1;
13+
return 0;
14+
}
15+
16+
// Usage:
17+
xor_upto_n(5); // Returns: 1 (1 ^ 2 ^ 3 ^ 4 ^ 5 = 1)
18+
```

snippets/cpp/debuging/vector-print.md

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Binary to Decimal Conversion
3+
description: Converts a binary number represented as a string to its decimal equivalent.
4+
tags: binary, conversion
5+
author: ashukr07
6+
---
7+
8+
```cpp
9+
int binary_to_decimal(const std::string& binary) {
10+
int decimal = 0;
11+
int base = 1; // Base value for the least significant bit
12+
13+
for (int i = binary.length() - 1; i >= 0; --i) {
14+
if (binary[i] == '1') {
15+
decimal += base;
16+
}
17+
base *= 2; // Move to the next power of 2
18+
}
19+
return decimal;
20+
}
21+
22+
// Usage:
23+
std::string binary = "1011"; // Binary representation of 11
24+
binary_to_decimal(binary); // Returns: 11
25+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
title: Check Perfect Number
3+
description: Checks if a number is a perfect number. A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself).
4+
tags: math, perfect-number
5+
author: ashukr07
6+
---
7+
8+
```cpp
9+
bool is_perfect(int n) {
10+
int sum = 1; // 1 is a divisor for all n > 1
11+
for (int i = 2; i * i <= n; ++i) {
12+
if (n % i == 0) {
13+
sum += i;
14+
if (i != n / i) sum += n / i;
15+
}
16+
}
17+
return sum == n && n != 1;
18+
}
19+
20+
// Usage:
21+
is_perfect(28); // Returns: true
22+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
title: Compound Interest
3+
description: Calculates the compound interest for a given principal, rate, time, and number of times interest applied per time period.
4+
tags: math, finance
5+
author: ashukr07
6+
---
7+
8+
```cpp
9+
#include <cmath>
10+
11+
double compound_interest(double principal, double rate, double time, int n) {
12+
return principal * std::pow(1 + rate / n, n * time);
13+
}
14+
15+
// Usage:
16+
double principal = 1000.0; // Initial amount
17+
double rate = 0.05; // Annual interest rate (5%)
18+
double time = 2; // Time in years
19+
int n = 4; // Compounded quarterly
20+
compound_interest(principal, rate, time, n); // Returns: 1104.081632653061
21+
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Factorial of a Number
3+
description: Calculates the factorial of a given non-negative integer.
4+
tags: math, factorial
5+
author: ashukr07
6+
---
7+
8+
```cpp
9+
int factorial(int n) {
10+
if (n <= 1) return 1; // Base case
11+
return n * factorial(n - 1); // Recursive step
12+
}
13+
14+
// Usage:
15+
factorial(5); // Returns: 120
16+
```

0 commit comments

Comments
 (0)