Number Utilities - CSU677 - Shoolini U

Number Utilities

Leap Year Checker

A leap year is a year that is divisible by 4, except for years that are divisible by 100 but not by 400.

$$\text{{Leap year}} = (\text{{year}} \mod 4 = 0) \land (\text{{year}} \mod 100 \neq 0) \lor (\text{{year}} \mod 400 = 0)$$

function checkLeapYear() {
    const year = parseInt(document.getElementById('leapYearInput').value);
    const isLeap = (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0);
    document.getElementById('leapYearResult').textContent = isLeap ? `${year} is a leap year.` : `${year} is not a leap year.`;
}

Input Handling

Leap Year Calculation

Result Display

Prime Number Checker

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.

$$\text{{Prime number}} = n > 1 \land \forall d \in [2, \sqrt{n}], (n \mod d \neq 0)$$

function checkPrime() {
    const num = parseInt(document.getElementById('primeInput').value);
    if (num <= 1) {
        document.getElementById('primeResult').textContent = `${num} is not a prime number.`;
        return;
    }
    let isPrime = true;
    for (let i = 2; i <= Math.sqrt(num); i++) {
        if (num % i === 0) {
            isPrime = false;
            break;
        }
    }
    document.getElementById('primeResult').textContent = isPrime ? `${num} is a prime number.` : `${num} is not a prime number.`;
}

Input Handling

Prime Number Calculation

Result Display

Odd or Even Checker

A number is considered even if it is divisible by 2 without a remainder. If a number is not divisible by 2, it is considered odd.

$$\text{{Even number}} = (n \mod 2 = 0)$$

$$\text{{Odd number}} = (n \mod 2 \neq 0)$$

function checkOddEven() {
    const num = parseInt(document.getElementById('oddEvenInput').value);
    document.getElementById('oddEvenResult').textContent = num % 2 === 0 ? `${num} is an even number.` : `${num} is an odd number.`;
}

Input Handling

Odd or Even Calculation

Result Display

Factorial Calculator

The factorial of a non-negative integer \( n \) is the product of all positive integers less than or equal to \( n \). The factorial is commonly used in permutations, combinations, and other areas of mathematics.

$$n! = n \times (n-1) \times (n-2) \times \ldots \times 1$$

function calculateFactorial() {
    const num = parseInt(document.getElementById('factorialInput').value);
    let result = 1;
    for (let i = 2; i <= num; i++) {
        result *= i;
    }
    document.getElementById('factorialResult').textContent = `${num}! = ${result}`;
}

Input Handling

Factorial Calculation

Result Display

GCD (Greatest Common Divisor) Calculator

The Greatest Common Divisor (GCD) of two numbers is the largest positive integer that divides both numbers without leaving a remainder. The GCD is useful in simplifying fractions and is a key concept in number theory.

$$\text{GCD}(a, b) = \max\{d : d | a \land d | b\}$$

function calculateGCD() {
    const num1 = parseInt(document.getElementById('gcdInput1').value);
    const num2 = parseInt(document.getElementById('gcdInput2').value);
    const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
    const result = gcd(num1, num2);
    document.getElementById('gcdResult').textContent = `GCD(${num1}, ${num2}) = ${result}`;
}

Input Handling

GCD Calculation Using the Euclidean Algorithm

Result Display

LCM (Least Common Multiple) Calculator

The Least Common Multiple (LCM) of two numbers is the smallest positive integer that is divisible by both numbers. The LCM is useful in adding or subtracting fractions with different denominators and is an important concept in number theory.

$$\text{LCM}(a, b) = \frac{|a \times b|}{\text{GCD}(a, b)}$$

function calculateLCM() {
    const num1 = parseInt(document.getElementById('lcmInput1').value);
    const num2 = parseInt(document.getElementById('lcmInput2').value);
    const gcd = (a, b) => (b === 0 ? a : gcd(b, a % b));
    const lcm = (a, b) => Math.abs(a * b) / gcd(a, b);
    const result = lcm(num1, num2);
    document.getElementById('lcmResult').textContent = `LCM(${num1}, ${num2}) = ${result}`;
}

Input Handling

LCM Calculation Using the GCD

Result Display

Square Root Calculator

The square root of a number \( n \) is a value \( x \) such that \( x^2 = n \). In other words, it is a number \( x \) that, when multiplied by itself, gives \( n \). The square root is a fundamental operation in mathematics, especially in algebra and geometry.

$$\sqrt{n} = x \quad \text{where} \quad x^2 = n$$

function calculateSquareRoot() {
    const num = parseInt(document.getElementById('sqrtInput').value);
    const result = Math.sqrt(num);
    document.getElementById('sqrtResult').textContent = `√${num} = ${result}`;
}

Input Handling

Square Root Calculation

Result Display

Exponentiation Calculator

Exponentiation is the mathematical operation involving two numbers, the base \( a \) and the exponent \( b \). It represents repeated multiplication of the base. For example, \( a^b \) means multiplying \( a \) by itself \( b \) times.

$$a^b = a \times a \times \ldots \times a \quad \text{(b times)}$$

function calculateExponent() {
    const base = parseInt(document.getElementById('baseInput').value);
    const exponent = parseInt(document.getElementById('exponentInput').value);
    const result = Math.pow(base, exponent);
    document.getElementById('exponentResult').textContent = `${base}^${exponent} = ${result}`;
}

Input Handling

Exponentiation Calculation

Result Display

Sum of Digits Calculator

The sum of the digits of a number is the result of adding together all the individual digits that make up the number. This operation is useful in various algorithms and is often used in digital root calculations and checksums.

$$\text{Sum of digits} = \sum_{i=1}^{n} d_i \quad \text{where } d_i \text{ is the i-th digit of the number}$$

function calculateSumOfDigits() {
    const num = parseInt(document.getElementById('sumDigitsInput').value);
    const sum = num.toString().split('').reduce((acc, digit) => acc + parseInt(digit), 0);
    document.getElementById('sumDigitsResult').textContent = `Sum of digits of ${num} = ${sum}`;
}

Input Handling

Sum of Digits Calculation

Result Display

Reverse a Number

Reversing a number involves rearranging its digits in reverse order. For example, if the number is 1234, the reversed number will be 4321. This operation is useful in various computational problems and algorithms.

If the number is \( n \), and its digits are \( d_1, d_2, \ldots, d_k \), then the reversed number is \( d_k, d_{k-1}, \ldots, d_1 \).

function reverseNumber() {
    const num = parseInt(document.getElementById('reverseInput').value);
    const reversed = parseInt(num.toString().split('').reverse().join(''));
    document.getElementById('reverseResult').textContent = `Reversed number of ${num} = ${reversed}`;
}

Input Handling

Number Reversal Process

Result Display