Tower of Hanoi:
How it works 🧠:
Move n-1 disks to the helper rod,
move the largest disk to the destination,
then move the n-1 disks from helper to destination.
Repeat recursively!
#Programming#coding#LearnAndGrow#recursion#TOWER_of_HANOI
Why it works:
A power of 2 has only one 1 in its binary form.
Subtracting 1 flips that 1 and everything after it.
Using n & (n - 1) clears the only set bit — result is 0!
Example:
8 (1000) & 7 (0111) = 0000 → Yes, 8 is a power of 2!
10 (1010) & 9 (1001) = 1000 → Nope, 10 isn’t.
Bitwise operators in C++ 🔧
& → AND
| → OR
^ → XOR
~ → NOT
<< → Left Shift
→ Right Shift
Control bits like a pro — fast, efficient, and powerful.
Master these for systems, graphics & performance!
#CPP#Coding#Bitwise#DevTips#learning
🧠 How it works:
Assume all numbers from 0 to n are prime.
Mark 0 and 1 as not prime.
Start from 2, the first prime.
For each prime i, mark all multiples of i (starting from i*i) as not prime.
Repeat until i*i > n.
Remaining true values are the primes! ✅
#DSA#Maths#cpp
Checks if a number is prime:
– Returns false for 0 & 1
– True for 2 & 3
– Eliminates multiples of 2 & 3
– Checks divisibility from 5 to √n, skipping by 6 (i.e., 5, 7, 11, 13...)
Efficient and avoids unnecessary checks.
#PrimeNumber#CodingTips#Algorithms#Logic#EfficientCode
How to Count Trailing Zeros in n!
1️⃣ Initialize res to 0 to store trailing zeros.
2️⃣ Loop through powers of 5 (5, 25, 125...) ≤ n.
3️⃣ Add n / i to res (counts multiples of each 5 power).
4️⃣ Return res—total trailing zeros in n!
#Coding#Math#Algorithms#Programming#DSA
Given input n = 3456:
Each iteration removes the last digit (n = n / 10) and increments count.
Steps: 3456 → 345 → 34 → 3 → 0
Loop stops when n > 0 is false.
Final count = 4 (number of digits).
Time Complexity: Θ(d), where d = digit count.
#Coding#tech#Trending#learn