← Back to Patterns
Math & BitEasy

Math

Number theory, modular arithmetic, and pattern recognition in numbers.

How it works

Math problems require recognizing number-theoretic properties and applying formulas directly. Key concepts: (1) GCD/LCM — use Euclidean algorithm: gcd(a, b) = gcd(b, a % b). LCM = a * b / gcd(a, b). (2) Prime checking — trial division up to √n is O(√n). Sieve of Eratosthenes finds all primes up to n in O(n log log n). (3) Modular arithmetic — (a + b) % m = ((a % m) + (b % m)) % m. Use mod to prevent overflow. (4) Digit manipulation — extract digits with % 10 and // 10. Reverse integer by building it digit by digit. (5) Pattern recognition — many math problems have a pattern visible from small cases (powers of 2, triangular numbers, etc.).

Interactive Playground

n1234%10 →result0

Reverse digits of integer 1234. Use modulo and division.

Speed

When to use

  • GCD, LCM, prime checking
  • Digit manipulation and number reversal
  • Modular arithmetic for large numbers

Tips

1Check small cases manually (n=1 to 10) to spot a pattern before writing a loop.
2Use integer arithmetic carefully: 10^18 overflows 64-bit integers in some cases — use Python's arbitrary precision or Java's BigInteger.
3For overflow-safe reversal, check before multiplying: `result > (INT_MAX - digit) / 10`.
4Modular exponentiation (fast power) computes a^b % m in O(log b) using repeated squaring.

Common Mistakes

!Integer overflow when multiplying large numbers — use long or check bounds before each multiply.
!Forgetting negative number edge cases for digit reversal or modular arithmetic.
!Using float division instead of integer division — introduces precision errors.
!Checking primality by trial division up to n instead of √n — unnecessary O(n) instead of O(√n).
💡 Key Insight:Look for patterns in small cases first — math problems often have elegant O(1) solutions.