<meta charset="UTF-8"><title>平方根簡略化装置</title><script>function isInteger(str) {
  return /^[0-9]+$/.test(str);
}
function removeGreaterThan(array, threshold) {
  return array.filter(num => num <= threshold);
}
function divideByTwo(number) {
  const quotient = Math.floor(number / 2);
  return quotient;
}

function multiplyAll(arr) {
  return arr.reduce((accumulator, currentValue) => accumulator * currentValue, 1);
}
function primeFactorization(num) {
  const factors = [];
  let divisor = 2;

  while (num > 1) {
    if (num % divisor === 0) {
      factors.push(divisor);
      num /= divisor;
    } else {
      divisor++;
    }
  }

  return factors;
}
function getRemainderByTwo(number) {
  return number % 2;
}

function isPrime(num) {
  if (num <= 1) return false;
  if (num === 2) return true;
  
  for (let i = 2; i <= Math.sqrt(num); i++) {
    if (num % i === 0) return false;
  }
  
  return true;
}
document.title = "平方根簡略化装置";
let bangou = Number(prompt("√xのxの値を整数で入力してください"));
if(isInteger(bangou)){
alert("√" + bangou + "を処理します。")
if(bangou == 0){
alert("√0");
location.reload();
}else{
if(isPrime(bangou)){
alert("√" + bangou);
location.reload();
}else{


const arr = primeFactorization(bangou);
const count = arr.reduce((prev, current) => {
  prev[current] = (prev[current] || 0) + 1;
  return prev;
}, {});
let amari = [];

Object.entries(count).forEach(([key, value]) => {
  if(getRemainderByTwo(value) == 1){
  amari.push(key);
  }
});
let kazu = {};
Object.entries(count).forEach(([key, value]) => {
  kazu[key] = divideByTwo(value);
});
let goukei = [];
Object.entries(kazu).forEach(([key, value]) => {
  if(value == 0){}else{
  goukei.push(key ** value);
  }
});
if(multiplyAll(amari) == 1){
alert(multiplyAll(goukei));
location.reload();
}else{
if(multiplyAll(goukei) == 1){
alert("√" + multiplyAll(amari));
location.reload();
}else{
alert(multiplyAll(goukei) + "√" + multiplyAll(amari));
location.reload();
}
}
}
}
}else{
alert("整数以外を入力しないでください、あなたのその行動は愚かです。");
location.reload();
}</script>