Math & Logic Validators

Mathematical validators for prime numbers, factorials, GCD, and numeric type checking.

25 code snippets available

Is Prime Number

math

Efficiently checks if a number is prime.

#algorithm#number
ts
const isPrime = (n: number): boolean => {
  if (n <= 1) return false;
  if (n <= 3) return true;
  if (n % 2 === 0 || n % 3 === 0) return false;
  for (let i = 5; i * i <= n; i += 6) {
    if (n % i === 0 || n % (i + 2) === 0) return false;
  }
  return true;
};

Is Even

math

Checks if a number is even.

#basic#number
ts
const isEven = (n: number): boolean => n % 2 === 0;

Is Odd

math

Checks if a number is odd.

#basic#number
ts
const isOdd = (n: number): boolean => n % 2 !== 0;

Is Power of Two

math

Bitwise check if number is a power of 2.

#bitwise#binary#optimization
ts
const isPowerOfTwo = (n: number): boolean => n > 0 && (n & (n - 1)) === 0;

GCD (Greatest Common Divisor)

math

Euclidean algorithm for GCD.

#algorithm#math
ts
const gcd = (a: number, b: number): number => (!b ? a : gcd(b, a % b));

Clamp Number

math

Restricts a number within a min and max range.

#utility#bounds
ts
const clamp = (num: number, min: number, max: number): number => Math.min(Math.max(num, min), max);

Random Integer In Range

math

Generates a random integer between min and max (inclusive).

#random#rng
ts
const randomInt = (min: number, max: number): number => Math.floor(Math.random() * (max - min + 1)) + min;

Factorial

math

Calculates n! (recursive).

#algorithm#recursive
ts
const factorial = (n: number): number => (n <= 1 ? 1 : n * factorial(n - 1));

Fibonacci

math

Returns nth Fibonacci number.

#algorithm#sequence
ts
const fib = (n: number): number => n <= 1 ? n : fib(n - 1) + fib(n - 2);

Is Positive

math

Checks if number is positive.

#basic#number
ts
const isPositive = (n: number): boolean => n > 0;

Is Negative

math

Checks if number is negative.

#basic#number
ts
const isNegative = (n: number): boolean => n < 0;

Is Integer

math

Checks if number is an integer.

#type#number
ts
const isInteger = (n: number): boolean => Number.isInteger(n);

Is Float

math

Checks if number has decimal places.

#type#number
ts
const isFloat = (n: number): boolean => !Number.isInteger(n) && !isNaN(n);

Generate Range

math

Generates array of numbers in range.

#array#range#utility
ts
const range = (start: number, end: number, step = 1): number[] => 
  Array.from({ length: Math.ceil((end - start) / step) }, (_, i) => start + i * step);

Sum Array

math

Calculates sum of array numbers.

#array#sum#utility
ts
const sum = (arr: number[]): number => arr.reduce((a, b) => a + b, 0);

Average of Array

math

Calculates average of array numbers.

#array#average#utility
ts
const average = (arr: number[]): number => arr.reduce((a, b) => a + b, 0) / arr.length;

Median of Array

math

Calculates median of array numbers.

#array#statistics#utility
ts
const median = (arr: number[]): number => {
  const sorted = [...arr].sort((a, b) => a - b);
  const mid = Math.floor(sorted.length / 2);
  return sorted.length % 2 ? sorted[mid] : (sorted[mid - 1] + sorted[mid]) / 2;
};

Percentage

math

0-100 with optional decimal.

#percent#number
ts
const isValid = (s: string): boolean => /^(100(\.0+)?|[0-9]{1,2}(\.\d+)?)%?$/.test(s);

Scientific Notation

math

e.g. 1.23e-4

#science#number
ts
const isValid = (s: string): boolean => /^-?\d+(\.\d+)?[eE][+-]?\d+$/.test(s);

Roman Numeral

math

Valid Roman numeral.

#roman#number
ts
const isValid = (s: string): boolean => /^M{0,3}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/.test(s);

Positive Integer

math

Positive whole number.

#number#positive
ts
const isValid = (s: string): boolean => /^[1-9]\d*$/.test(s);

Negative Integer

math

Negative whole number.

#number#negative
ts
const isValid = (s: string): boolean => /^-[1-9]\d*$/.test(s);

Decimal Number

math

Number with decimals.

#number#decimal
ts
const isValid = (s: string): boolean => /^-?\d+\.\d+$/.test(s);

Binary String

math

Binary representation.

#binary#number
ts
const isValid = (s: string): boolean => /^[01]+$/.test(s);

Octal String

math

Octal representation.

#octal#number
ts
const isValid = (s: string): boolean => /^[0-7]+$/.test(s);

About Math & Logic Validators

Our math & logic validation snippets are designed to handle the most common validation scenarios you'll encounter in modern software development. Each snippet is thoroughly tested, optimized for performance, and follows industry best practices.

All validators are available in multiple programming languages including JavaScript, TypeScript, Python, Go, PHP, C#, and Rust. Simply copy the code snippet, adapt it to your specific needs, and integrate it into your project. Every snippet is MIT licensed—free for personal and commercial use.

Browse Other Categories