Simplifying Radicals Calculator
Three tools in one: simplify any square root by hunting perfect squares, add or subtract radicals
step by step, or rationalize a denominator. Every answer comes with the full working shown.
Simplify a square root
Enter a radicand. The calculator finds the largest perfect square hiding inside and pulls it out.
Add or subtract radicals
Enter two radical terms. Each is simplified first — then combined if the radicands match.
Rationalize a denominator
Enter p/√q. The calculator multiplies by √q/√q and reduces the result.
/* ---------------- pure logic (tested in node) ---------------- */ function gcd(a, b){ a = Math.abs(a); b = Math.abs(b); while (b !== 0){ var t = a % b; a = b; b = t; } return a === 0 ? 1 : a; } function escHTML(s){ return String(s).replace(/[&<>"']/g, function(c){ return {'&':'&','<':'<','>':'>','"':'"',"'":'''}[c]; }); } function simplifyRadical(n){ if (n < 1 || Math.floor(n) !== n) return null; for (var i = Math.floor(Math.sqrt(n)); i >= 2; i--){ var sq = i * i; if (n % sq === 0) return { c:i, r:n / sq }; } return { c:1, r:n }; } function radHTMLd(c, r){ if (r === 1) return '' + c + ''; return '' + (c === 1 ? '' : c) + '√' + r + ''; } function validPosInt(s, minV){ s = String(s).trim(); if (!/^[0-9]+$/.test(s)) return null; var v = parseInt(s, 10); if (v < minV) return null; if (v > 1000000) return null; return v; } /* Tab 1: simplify sqrt(n) */ function simplifySteps(nRaw){ var n = validPosInt(nRaw, 1); if (n === null) return { ok:false, err:'Enter a positive whole-number radicand (for example 72).' }; var s = simplifyRadical(n); var steps = []; steps.push('Hunt: the largest perfect square dividing ' + n + ' is ' + s.c + '² = ' + (s.c * s.c) + '.'); if (s.c === 1){ steps.push(n + ' has no perfect-square divisor bigger than 1, so it is already simplified.'); return { ok:true, steps:steps, ansHTML:radHTMLd(1, n), ansText:'√' + n }; } steps.push('Split: √' + n + ' = √(' + (s.c * s.c) + '·' + s.r + ').'); steps.push('Pull out: √' + (s.c * s.c) + ' = ' + s.c + ', so the answer is ' + s.c + '√' + s.r + '.'); steps.push('Check: (' + s.c + '√' + s.r + ')² = ' + (s.c * s.c) + '·' + s.r + ' = ' + n + '.'); var finH = s.r === 1 ? '' + s.c + '' : radHTMLd(s.c, s.r); var finT = s.r === 1 ? String(s.c) : (s.c === 1 ? '' : s.c) + '√' + s.r; return { ok:true, steps:steps, ansHTML:finH, ansText:finT }; } /* Tab 2: a√m ± b√n */ function addRadSteps(aRaw, mRaw, op, bRaw, nRaw){ var a = validPosInt(aRaw, 0), m = validPosInt(mRaw, 1); var b = validPosInt(bRaw, 0), n = validPosInt(nRaw, 1); if (a === null || m === null || b === null || n === null) return { ok:false, err:'Enter whole numbers: coefficients (0 or more) and radicands (1 or more).' }; if (!(op === 'add' || op === 'sub')) return { ok:false, err:'Choose add or subtract.' }; var s1 = simplifyRadical(m), s2 = simplifyRadical(n); var c1 = a * s1.c, c2 = b * s2.c; var steps = []; steps.push('Simplify the first term: ' + a + '√' + m + ' = ' + a + '·' + s1.c + '√' + s1.r + ' = ' + c1 + '√' + s1.r + '.'); steps.push('Simplify the second term: ' + b + '√' + n + ' = ' + b + '·' + s2.c + '√' + s2.r + ' = ' + c2 + '√' + s2.r + '.'); var opSym = (op === 'add') ? '+' : '−'; if (s1.r === s2.r){ var c = (op === 'add') ? c1 + c2 : c1 - c2; steps.push('Same radicand (' + s1.r + ') — combine the coefficients: ' + c1 + ' ' + opSym + ' ' + c2 + ' = ' + c + '.'); var ansHTML = radHTMLd(Math.abs(c), s1.r); var sign = c < 0 ? '-' : ''; var mag = Math.abs(c); if (s1.r === 1) ansHTML = '' + c + ''; else ansHTML = (c < 0 ? '-' : '') + '' + (mag === 1 ? '' : mag) + '√' + s1.r + ''; return { ok:true, steps:steps, ansHTML:ansHTML, ansText: c + '√' + s1.r }; } steps.push('Different radicands (' + s1.r + ' vs ' + s2.r + ') — these are not like terms and cannot combine.'); var expr = c1 + '√' + s1.r + ' ' + opSym + ' ' + c2 + '√' + s2.r; return { ok:true, steps:steps, ansHTML:'' + escHTML(expr).replace(/√/g, '√') + '', ansText:expr }; } /* Tab 3: rationalize p/√q */ function rationalizeSteps(pRaw, qRaw){ var p = validPosInt(pRaw, 1), q = validPosInt(qRaw, 2); if (p === null || q === null) return { ok:false, err:'Enter whole numbers: p ≥ 1 and q ≥ 2.' }; var s = simplifyRadical(q); var steps = []; steps.push('Multiply top and bottom by √' + q + ' (that is, multiply by 1).'); steps.push('Denominator: √' + q + ' · √' + q + ' = ' + q + '.'); var num = p * s.c, den = q, rad = s.r; if (s.c !== 1) steps.push('Tidy the numerator’s radical: √' + q + ' = ' + s.c + '√' + rad + ', so the fraction is (' + num + '√' + rad + ')/' + den + '.'); else steps.push('The fraction is now (' + num + '√' + rad + ')/' + den + '.'); var g = gcd(num, den); var nc = num / g, nd = den / g; if (g !== 1) steps.push('Reduce ' + num + '/' + den + ' — divide top and bottom by ' + g + '.'); else steps.push('The fraction ' + num + '/' + den + ' is already reduced.'); var ansHTML = '' + (nc === 1 ? '' : nc) + '√' + rad + '' + '' + nd + ''; if (rad === 1){ ansHTML = '' + (nc / nd) + ''; steps.push('√' + q + ' was a perfect square after all: the answer is just ' + (nc / nd) + '.'); } return { ok:true, steps:steps, ansHTML:ansHTML, ansText:'(' + nc + '√' + rad + ')/' + nd }; }
/* ---------------- UI wiring ---------------- */ function el(id){ return document.getElementById(id); } function renderCalc(res, errId, resId, ansId, stepsId){ var err = el(errId), res = el(resId); if (!res.ok){ err.textContent = res.err; err.classList.add('show'); res.classList.remove('show'); return; } err.classList.remove('show'); el(ansId).innerHTML = 'Answer: ' + res.ansHTML + ''; el(stepsId).innerHTML = res.steps.map(function(s){ return '
'; }).join(''); res.classList.add('show'); } function initTool(){ var tabs = document.querySelectorAll('.tabbtn'); for (var i = 0; i < tabs.length; i++){ tabs[i].addEventListener('click', function(){ for (var j = 0; j < tabs.length; j++) tabs[j].classList.remove('active'); this.classList.add('active'); var panels = document.querySelectorAll('.panel'); for (var k = 0; k < panels.length; k++) panels[k].classList.remove('active'); document.getElementById(this.getAttribute('data-tab')).classList.add('active'); }); } el('t1go').addEventListener('click', function(){ renderCalc(simplifySteps(el('t1n').value), 't1err', 't1res', 't1ans', 't1steps'); }); el('t2go').addEventListener('click', function(){ renderCalc(addRadSteps(el('t2a').value, el('t2m').value, el('t2op').value, el('t2b').value, el('t2n').value), 't2err', 't2res', 't2ans', 't2steps'); }); el('t3go').addEventListener('click', function(){ renderCalc(rationalizeSteps(el('t3p').value, el('t3q').value), 't3err', 't3res', 't3ans', 't3steps'); }); } if (!(typeof document === 'undefined' || !document.addEventListener)){ document.addEventListener('DOMContentLoaded', initTool); }