} $factor = 3; $maxFactor = sqrt($n); while ($n > 1 && $factor <= $maxFactor) { if ($n % $factor == 0) { $factorArray[$factor] = 1; //echo "\t".$n."\t".$factor."\n"; $n = $n / $factor; $lastFactor = $factor; while ($n % $factor == 0) { $factorArray[$factor] += 1; //echo "\t".$n."\t".$factor."\n"; $n = $n / $factor; } $maxFactor = sqrt($n); } $factor = $factor + 2; } if ($n != 1) { $lastFactor = $n; $factorArray[$n] = 1; } return numberOfDivisors($factorArray); } $factors = $triangle = $i = $divisors = 0; while ($divisors < 500) { $i++; $triangle += $i; $divisors = factor($triangle); echo $triangle . "\t" . $divisors . "\n"; }
* if the number is divisible by two, then it's not prime and it's no longer * needed to check other even numbers */ if ($num % 2 == 0) { return false; } /** * Checks the odd numbers. If any of them is a factor, then it returns false. * The sqrt can be an aproximation, hence just for the sake of * security, one rounds it to the next highest integer value. */ for ($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) { if ($num % $i == 0) { return false; } } return true; } $t = fgets($_fp); for ($i = 0; $i < $t; $i++) { $num = trim(fgets($_fp)); $fact_array = factor($num); $fact_array = array_unique($fact_array); $fact_array = array_filter($fact_array, 'isPrime'); if (count($fact_array) > 0) { echo max($fact_array); } else { echo $num; } echo "\n"; }