Posts

Showing posts from February, 2025

6. Maths- part 2

Image
8.) Print all divisors/factors- Brute:  1 se n          tak ka loop chalake check divisibility of n with each i Optimal: 1 se sqrt(n) tak ka loop chalado and print i and n/i as per divisibility  ----------------------------------------------------------------------------------------------------------------------------- 9.)  Prime number check- prime number- has exactly 2 divisors (1 and itself) Brute:  1 se n          tak dekhlo, ki it must have exactly 2 divisors... Optimal:   1 se sqrt(n) tak dekhlo, ki it must have exactly 1 divisor...⭐ above methods ki cond true hai toh prime hoga...else not... ----------------------------------------------------------------------------------------------------------------------------- 10.) Prime factors of n: Brute:      find all factors of n, then filter out prime numbers out of them.   Optimal:  finding all factors wale code ko hi modify krdo⭐ ---...

23. Bit manipulation

Image
Note :- 1. decimal to binary  &   binary to decimal conversion ---------------- 2. int type variable stores data upto 32 bits, so if value is of x bits only then remaining 32-x bits are 0 ----------------- 3. One's complement - flip all bits of the number in binary form      Two's complement - add 1 to the one's complement. ------------------ 4.  (-ve)  integers :     stored as 2s complement ⭐     (-ve) decimals :   an extra bit is appended at MSB of binary form of number where,                                0 for (+) and 1 for (-)⭐ ------------------------- 5. XOR : ⭐ 1^1=0,      1^0=1 a^a = 0,     a^0 = a ------------------------- 6. NOT :⭐ ~x = 1s complement of x in decimal form: (~x = -x-1) converting ~x from binary to decimal:- - if (MSB=1) then, ~x is a negative number => take 2s complement ...