Jump to content

Prime number Check


JacksonSoccerBoy

Recommended Posts

I need to create a snippet of code to check an incoming number to see if its prime or not. this is what I have but for some reason I can't get it to run. Jus needs another set of eyes to look it over, thanks in advance

 

<html>
<body>

<?php


$number = $_GET["prime"];
$prime=0;


for ($i=2; $i<=($number / 2); $i++) {
if($number % $i == 0) { 
$prime=1;
}
}
$prime=2;

IF ($prime == 1){
echo "$number is prime";
}
ELSE{
echo "$number is not prime";
}






?>
</body>
</html> 

Link to comment
https://forums.phpfreaks.com/topic/132321-prime-number-check/
Share on other sites

function factors($value) {
$startVal = floor(sqrt($value));

$factorArray = array();
for ($i = $startVal; $i > 1; --$i) {
	if (($value % $i) == 0) {
		$factorArray = array_merge($factorArray,factors($value / $i));
		$factorArray = array_merge($factorArray,factors($i));
		if ($i <= sqrt($value)) {
			break;
		}
	}
}
if (count($factorArray) > 0) {
	return $factorArray;
}
return array((integer) $value);
}

function isPrime($value) {
if (count(factors($value)) == 1) {
	return True;
}
return False;
}

Link to comment
https://forums.phpfreaks.com/topic/132321-prime-number-check/#findComment-688006
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.