From your comments above, you're looking for a way to calculate mathematically exact square roots. This problem belongs to the domain of symbolic computation and cannot be solved with floating-point manipulations. Here's a (deliberately simplified) example of how to take a square root symbolically:
function factorize($n) {
$factors = array();
$p = 2;
while($n > 1) {
if($n % $p == 0) {
$factors[$p]++;
$n = intval($n / $p);
} else $p++;
}
return $factors;
}
function symbolic_root($n) {
$rat = $irr = 1;
foreach(factorize($n) as $prime => $power) {
$rat *= pow($prime, intval($power / 2));
$irr *= pow($prime, intval($power % 2));
}
if($irr == 1) return $rat;
if($rat == 1) return "sqrt $irr";
return "$rat * sqrt($irr)";
}
echo symbolic_root(1522756), "\n"; # prints "1234"
echo symbolic_root(5549544), "\n"; # prints "462 * sqrt(26)"
Explanation for those curious:
First, we factor the number into the prime powers:
5549544 = 23×32×72×112×131
then, divide each power by two, which gives us the rational part of the root:
462 = 21×31×71×111×130
and the rests (1s and 0s) form the irrational part
26 = 21×30×70×110×131