JSFiddle - React, Tailwind, and code Playground
by darkajax
HTML
<label for="n">Type number to get prime factors: </label>
<input type="text" id="n" />
<button id="check">Check</button>
<br>
<div id="result">
</div>
CSS
body {
background: black;
color: orange;
}
label, input, button, div {
font-family: monospace;
font-size: 15px;
margin: 5px;
}
#n {
background: black;
color: orange;
}
#check {
background: black;
border: 3px solid orange;
color: orange;
}
#result {
border: 3px solid orange;
display: inline-block;
height: 18px;
padding: 5px;
text-align: center;
width: 570px;
}
JavaScript
document.getElementById("check").addEventListener("click", checkPrime);
function checkPrime() {
let n = document.getElementById("n").value;
if (n && !isNaN(n)) {
document.getElementById("result").innerHTML = primeFactors(n);
}
}
function primeFactors(n){
n = BigInt(n);
let factors = [];
let divisor = BigInt("2");
while (n >= divisor) {
if (n % divisor == 0) {
factors.push(divisor);
n = n / divisor;
} else {
divisor++;
}
}
return factors.join(";");
}
alert(primeFactors(8));
alert(primeFactors(12311231));
alert(primeFactors(123123));
alert(primeFactors(Number.MAX_SAFE_INTEGER));
alert(primeFactors(9007199254740991123123123));