Skip to content

Instantly share code, notes, and snippets.

@nermin99
Last active June 13, 2022 07:01
Show Gist options
  • Save nermin99/435189d22f3cbd01ac1a05fbb9aa5d38 to your computer and use it in GitHub Desktop.
Save nermin99/435189d22f3cbd01ac1a05fbb9aa5d38 to your computer and use it in GitHub Desktop.
Prime Factorizer
<html lang="en">
<head>
<title>Prime Factorizer</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<form method="post">
<label>Input</label><br>
<input type="text" name="input" autocomplete="off" autofocus><br>
<input type="submit" value="Factorize"><br>
</form>
<?php
if (isset($_POST["input"])) {
$input = htmlspecialchars($_POST["input"]);
$n = $input;
$i = 2;
$iterations = 0;
echo $input;
while ($i <= floor(sqrt($n))) {
if ($n % $i == 0) { // if factor is divisible by the prime
$primes[] = $i; // add the prime to array
$n /= $i; // get the next factor
} else if ($i == 2) { // increment until next prime is found
$i++;
} else { // if factor isn't even, increment by 2
$i += 2;
}
$iterations++;
}
$primes[] = $n; // only necessary to check primes up to sqrt of factor, then factor itself is prime
echo " = ";
if($input < 2) exit($input);
for ($j = 0; $j < count($primes); $j++) {
echo "$primes[$j] ";
}
if(count($primes) == 1) echo "<br><b>prime</b>";
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment