๐Ÿงฎ Day 10 - Number Theory

Logic, Concepts, and Deep Understanding

๐ŸŽฏWhat is Number Theory?

๐Ÿง  Core Concept:

Number theory is the branch of mathematics that studies the properties and relationships of integers. In programming interviews, it helps us solve problems involving:

  • Prime numbers - Building blocks of all integers
  • Divisibility - Understanding when one number divides another
  • Modular arithmetic - Working with remainders efficiently
  • Greatest Common Divisor (GCD) - Finding common factors

๐Ÿค” Why Learn This?

Number theory appears in:

  • Cryptography and security algorithms
  • Hash functions and data structures
  • Algorithm optimization techniques
  • Mathematical problem-solving in competitive programming

๐Ÿ”ขPrime Numbers: The Building Blocks

๐Ÿ“š What is a Prime Number?

A prime number is a natural number greater than 1 that has exactly two factors: 1 and itself.

Examples: 2, 3, 5, 7, 11, 13, 17, 19, 23...
Not Prime: 1 (only one factor), 4 (factors: 1,2,4), 9 (factors: 1,3,9)

๐Ÿง  Logic Behind Prime Checking

๐Ÿ’ก Naive Approach Logic:

To check if n is prime, test if any number from 2 to n-1 divides n.

Step 1: If n < 2, return False (by definition)
Step 2: For i from 2 to n-1, check if n % i == 0
Step 3: If any i divides n, return False
Step 4: If no divisor found, return True
Time Complexity: O(n) - We check every number
Problem: Very slow for large numbers!

โšก Optimized Prime Checking Logic

๐Ÿ’ก Key Insight:

If n has a factor greater than โˆšn, it must also have a factor less than โˆšn!

๐Ÿค” Why โˆšn Works:

Consider n = a ร— b where a โ‰ค b

  • If both a > โˆšn and b > โˆšn, then a ร— b > โˆšn ร— โˆšn = n
  • This is impossible since a ร— b = n
  • Therefore, at least one factor must be โ‰ค โˆšn

๐ŸŽฏ Example: Is 49 prime?

โˆš49 = 7, so we only check divisors up to 7

  • 49 รท 2 = 24.5 (not divisible)
  • 49 รท 3 = 16.33... (not divisible)
  • 49 รท 4 = 12.25 (not divisible)
  • 49 รท 5 = 9.8 (not divisible)
  • 49 รท 6 = 8.16... (not divisible)
  • 49 รท 7 = 7 โœ“ (divisible!)

Result: 49 is not prime (49 = 7 ร— 7)

Time Complexity: O(โˆšn) - Much faster!

๐Ÿญ Sieve of Eratosthenes: Bulk Prime Generation

๐Ÿ’ก The Big Idea:

Instead of checking each number individually, eliminate all composite numbers systematically.

Step 1: Create array [2, 3, 4, 5, 6, 7, 8, 9, 10, 11...]
Step 2: Start with first prime (2)
Step 3: Mark all multiples of 2 as composite (4, 6, 8, 10...)
Step 4: Move to next unmarked number (3)
Step 5: Mark all multiples of 3 as composite (9, 15, 21...)
Step 6: Repeat until โˆšn

๐ŸŽจ Visual Example: Sieve up to 30

Initial: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30] After 2: [2, 3, โœ—, 5, โœ—, 7, โœ—, 9, โœ—, 11, โœ—, 13, โœ—, 15, โœ—, 17, โœ—, 19, โœ—, 21, โœ—, 23, โœ—, 25, โœ—, 27, โœ—, 29, โœ—] After 3: [2, 3, โœ—, 5, โœ—, 7, โœ—, โœ—, โœ—, 11, โœ—, 13, โœ—, โœ—, โœ—, 17, โœ—, 19, โœ—, โœ—, โœ—, 23, โœ—, 25, โœ—, โœ—, โœ—, 29, โœ—] After 5: [2, 3, โœ—, 5, โœ—, 7, โœ—, โœ—, โœ—, 11, โœ—, 13, โœ—, โœ—, โœ—, 17, โœ—, 19, โœ—, โœ—, โœ—, 23, โœ—, โœ—, โœ—, โœ—, โœ—, 29, โœ—] Final Primes: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Time Complexity: O(n log log n)
Space Complexity: O(n)
Best For: Finding all primes up to a limit

๐Ÿ”—GCD & LCM: Finding Common Ground

๐Ÿ“š Definitions:

GCD (Greatest Common Divisor): Largest number that divides both a and b

LCM (Least Common Multiple): Smallest number that both a and b divide

GCD(a, b) ร— LCM(a, b) = a ร— b

๐Ÿง  Euclidean Algorithm Logic

๐Ÿ’ก The Brilliant Insight:

GCD(a, b) = GCD(b, a mod b)

๐Ÿค” Why This Works:

If d divides both a and b, then:

  • a = d ร— kโ‚ for some integer kโ‚
  • b = d ร— kโ‚‚ for some integer kโ‚‚
  • a mod b = a - (aรทb) ร— b = d ร— kโ‚ - (aรทb) ร— d ร— kโ‚‚
  • So d also divides (a mod b)

This means any common divisor of (a,b) is also a common divisor of (b, a mod b)

๐ŸŽฏ Example: GCD(48, 18)

GCD(48, 18) โ†’ 48 mod 18 = 12
GCD(18, 12) โ†’ 18 mod 12 = 6
GCD(12, 6) โ†’ 12 mod 6 = 0
GCD(6, 0) โ†’ Answer: 6

Verification: 48 = 6ร—8, 18 = 6ร—3, so GCD is indeed 6!

Time Complexity: O(log min(a, b))
Why so fast: Each step reduces the problem size by at least half!

๐Ÿ”„Modular Arithmetic: The Art of Remainders

๐Ÿ“š What is Modular Arithmetic?

Working with remainders instead of actual numbers. It's like clock arithmetic!

Notation: a โ‰ก b (mod m) means "a and b have the same remainder when divided by m"
Example: 17 โ‰ก 5 (mod 12) because both 17 and 5 leave remainder 5 when divided by 12

โšก Fast Modular Exponentiation

๐Ÿ’ก The Problem:

Computing a^b mod m directly can overflow for large numbers!

๐Ÿšจ Example Problem:

Calculate 2^1000 mod 1000000007

  • 2^1000 has about 301 digits!
  • No standard data type can hold this
  • We need a smarter approach

๐Ÿ’ก The Solution: Binary Exponentiation

Key insight: a^b = (a^(b/2))^2 when b is even

Step 1: If exponent is 0, return 1
Step 2: If exponent is odd, multiply result by base
Step 3: Square the base, halve the exponent
Step 4: Repeat until exponent becomes 0
Key: Take mod at each step to prevent overflow!

๐ŸŽฏ Example: 3^10 mod 7

Initial: base=3, exp=10, result=1, mod=7 Step 1: exp=10 (even), baseยฒ=(3ยฒ) mod 7 = 9 mod 7 = 2, exp=5 Step 2: exp=5 (odd), result=(1ร—2) mod 7 = 2, exp=4 Step 3: exp=4 (even), baseยฒ=(2ยฒ) mod 7 = 4, exp=2 Step 4: exp=2 (even), baseยฒ=(4ยฒ) mod 7 = 16 mod 7 = 2, exp=1 Step 5: exp=1 (odd), result=(2ร—2) mod 7 = 4, exp=0 Final Answer: 4

Verification: 3^10 = 59049, 59049 mod 7 = 4 โœ“

Time Complexity: O(log b)
Space Complexity: O(1)
Magic: Reduces 1000 multiplications to just 10!

ฯ†Euler's Totient Function

๐Ÿ“š Definition:

ฯ†(n) = count of integers from 1 to n that are coprime to n (GCD = 1)

Examples:
ฯ†(6) = 2 (numbers 1, 5 are coprime to 6)
ฯ†(9) = 6 (numbers 1, 2, 4, 5, 7, 8 are coprime to 9)

๐Ÿง  The Logic Behind the Formula

๐Ÿ’ก Key Insight:

For a prime p: ฯ†(p) = p - 1 (all numbers except multiples of p)

ฯ†(n) = n ร— โˆ(1 - 1/p) for all prime factors p of n

๐Ÿค” Why This Formula Works:

  • Start with all n numbers: {1, 2, 3, ..., n}
  • Remove multiples of first prime pโ‚: n - n/pโ‚ numbers left
  • Remove multiples of second prime pโ‚‚: multiply by (1 - 1/pโ‚‚)
  • Continue for all prime factors

๐ŸŽฏ Example: ฯ†(12)

12 = 2ยฒ ร— 3, so prime factors are 2 and 3

ฯ†(12) = 12 ร— (1 - 1/2) ร— (1 - 1/3)
= 12 ร— (1/2) ร— (2/3)
= 12 ร— 1/3 = 4

Verification: Numbers coprime to 12: {1, 5, 7, 11} โ†’ Count = 4 โœ“

๐Ÿ”ขDivisors and Perfect Numbers

๐Ÿ“š Understanding Divisors:

Divisors of n are all positive integers that divide n evenly.

Example: Divisors of 12 are {1, 2, 3, 4, 6, 12}

๐Ÿง  Smart Divisor Finding Logic

๐Ÿ’ก Key Insight:

Divisors come in pairs! If d divides n, then n/d also divides n.

Step 1: Only check up to โˆšn
Step 2: For each divisor i found, also add n/i
Step 3: Special case: if iยฒ = n, only add i once

๐ŸŽฏ Example: Find divisors of 36

โˆš36 = 6, so check 1, 2, 3, 4, 5, 6

  • 1 divides 36 โ†’ add 1 and 36/1 = 36
  • 2 divides 36 โ†’ add 2 and 36/2 = 18
  • 3 divides 36 โ†’ add 3 and 36/3 = 12
  • 4 divides 36 โ†’ add 4 and 36/4 = 9
  • 6 divides 36 โ†’ add 6 and 36/6 = 6 (but 6 = 6, so add once)

Result: {1, 2, 3, 4, 6, 9, 12, 18, 36}

Time Complexity: O(โˆšn)
Space Complexity: O(d(n)) where d(n) is number of divisors

โœจ Perfect Numbers

๐Ÿ“š Definition:

A perfect number equals the sum of its proper divisors (excluding itself).

๐ŸŽฏ Example: 6 is perfect

  • Divisors of 6: {1, 2, 3, 6}
  • Proper divisors: {1, 2, 3}
  • Sum: 1 + 2 + 3 = 6 โœ“

๐ŸŽฏ Example: 28 is perfect

  • Proper divisors: {1, 2, 4, 7, 14}
  • Sum: 1 + 2 + 4 + 7 + 14 = 28 โœ“

๐Ÿš€Real-World Applications & Interview Problems

๐ŸŽฏ Common Interview Patterns:

Problem Type Key Concept Example Problems
Prime Checking Optimized โˆšn algorithm Count Primes, Prime Factorization
Bulk Prime Finding Sieve of Eratosthenes Find all primes in range
Common Factors Euclidean Algorithm Simplify fractions, array GCD
Large Exponents Fast modular exponentiation Power calculations, RSA crypto
Counting Problems Euler's totient function Coprime counting, cyclic groups