Math

PRIME CHECKER

Check if a number is prime, find prime factors, and explore prime number patterns.


LIST PRIMES UP TO N

GOLDBACH'S CONJECTURE

Every even integer > 2 is the sum of two primes.

Developer Reference

Core Algorithm & Standalone Script

Standalone, zero-dependency JavaScript implementation powering this tool. Free to inspect, copy, and build upon.

function isPrime(n) {
    if (n < 2) return false;
    if (n < 4) 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;
  }

  function primeFactors(n) {
    const factors = {};
    let d = 2;
    while (d * d <= n) {
      while (n % d === 0) { factors[d] = (factors[d] || 0) + 1; n = Math.floor(n / d); }
      d++;
    }
    if (n > 1) factors[n] = (factors[n] || 0) + 1;
    return factors;
  }

  function prevPrime(n) {
    let c = n - 1;
    while (c > 1 && !isPrime(c)) c--;
    return c > 1 ? c : null;
  }

  function nextPrime(n) {
    let c = n + 1;
    while (!isPrime(c)) c++;
    return c;
  }

  function check() {
    const raw = document.getElementById('numInput').value.trim().replace(/,/g, '');
    const card = document.getElementById('resultCard');
    if (!raw) { card.classList.add('hidden'); return; }
    const n = parseInt(raw, 10);
    if (isNaN(n) || n < 0) { card.classList.add('hidden'); return; }
    card.classList.remove('hidden');

    const prime = isPrime(n);
    const badge = document.getElementById('resultBadge');
    badge.textContent = prime ? '✓ PRIME' : '✗ NOT PRIME';
    badge.className = 'result-badge ' + (prime ? 'prime' : 'not-prime');

    if (!prime && n > 1) {
      const factors = primeFactors(n);
      const parts = Object.entries(factors).map(([p, e]) => e > 1 ? `${p}<sup>${e}</sup>` : p);
      document.getElementById('factDisplay').innerHTML = `${n} = ${parts.join(' × ')}`;
    } else {
      document.getElementById('factDisplay').textContent = prime ? 'Cannot be factored further' : (n === 0 ? '0 is neither prime nor composite' : '1 is neither prime nor composite');
    }

    document.getElementById('statDigits').textContent = raw.length;
    const specials = [];
    if (n === 2) specials.push('Only even prime');
    document.getElementById('statSpecial').textContent = specials.join(', ');

    const pp = prevPrime(n);
    const np = nextPrime(n);
    document.getElementById('prevPrime').textContent = pp !== null ? pp : 'none';
    document.getElementById('nextPrime').textContent = np;
  }

  function loadNeighbor(dir) {
    const pp = document.getElementById(dir === 'prev' ? 'prevPrime' : 'nextPrime').textContent;
    if (pp && pp !== '—' && pp !== 'none') {
      document.getElementById('numInput').value = pp;
      check();
    }
  }

  function sieve() {
    let limit = parseInt(document.getElementById('sieveLimit').value);
    if (isNaN(limit) || limit < 2) limit = 2;
    if (limit > 100000) limit = 100000;
    const composite = new Uint8Array(limit + 1);
    for (let i = 2; i * i <= limit; i++) {
      if (!composite[i]) for (let j = i*i; j <= limit; j += i) composite[j] = 1;
    }
    const primes = [];
    for (let i = 2; i <= limit; i++) if (!composite[i]) primes.push(i);
    const el = document.getElementById('sieveResult');
    el.classList.remove('hidden');
    el.innerHTML = `<strong>${primes.length} primes up to ${limit}:</strong><br>${primes.join(', ')}`;
  }

  function goldbach() {
    const n = parseInt(document.getElementById('goldbachInput').value);
    const el = document.getElementById('goldbachResult');
    if (isNaN(n) || n < 4 || n % 2 !== 0) {
      el.innerHTML = 'Enter an even number ≥ 4.'; return;
    }
    const pairs = [];
    for (let a = 2; a <= n / 2; a++) {
      if (isPrime(a) && isPrime(n - a)) pairs.push([a, n - a]);
      if (pairs.length >= 5) break;
    }
    if (!pairs.length) { el.innerHTML = 'No pair found (unexpected!).'; return; }
    el.innerHTML = pairs.map(([a, b]) => `<span>${n}</span> = <span>${a}</span> + <span>${b}</span>`).join('<br>');
  }

  goldbach();