Math
Welcome to the Math section of JDCodebase! Many programming problems involve mathematical insights — whether it's number theory, bitwise operations, or pattern-based logic. Mastering math fundamentals makes you faster and more accurate in problem-solving.
What You’ll Learn
- Number properties: even/odd, primes, divisibility
- Factorials, combinations, and permutations
- Modular arithmetic and modulo inverse
- Bit manipulation basics: AND, OR, XOR, shifts
- Digit operations and numeric tricks in code
Common JavaScript Math Methods
Math.floor(x): Rounds a number down to the nearest integer.
Before: Math.floor(3.7) Code: Math.floor(3.7); After: 3
Math.ceil(x): Rounds a number up to the nearest integer.
Before: Math.ceil(3.1) Code: Math.ceil(3.1); After: 4
Math.round(x): Rounds a number to the nearest integer.
Before: Math.round(2.5) Code: Math.round(2.5); After: 3
Math.abs(x): Returns the absolute (positive) value.
Before: Math.abs(-7) Code: Math.abs(-7); After: 7
Math.max(a, b, ...): Returns the largest of zero or more numbers.
Before: Math.max(1, 4, 2) Code: Math.max(1, 4, 2); After: 4
Math.min(a, b, ...): Returns the smallest of zero or more numbers.
Before: Math.min(1, 4, 2) Code: Math.min(1, 4, 2); After: 1
Math.sqrt(x): Returns the square root of x.
Before: Math.sqrt(25) Code: Math.sqrt(25); After: 5
Math.pow(base, exponent): Returns base raised to the power of exponent.
Before: Math.pow(2, 3) Code: Math.pow(2, 3); After: 8
x % y: Returns the remainder of division.
Before: 10 % 3 Code: 10 % 3; After: 1
Bitwise Operators (&, |, ^, ~, <<, >>): Used for low-level manipulation and fast calculations.
Before: 5 & 3 Code: 5 & 3; After: 1
Try This Example
Check if a number is prime.
function isPrime(n) { if (n <= 1) return false; for (let i = 2; i <= Math.sqrt(n); i++) { if (n % i === 0) return false; } return true; }
Input: 17
Expected Output: true