SnippetsProject Code

Check Prime Numbers

Description

This function will check any number to see if it is a prime number or not!

Parameters

num
Any number to be checked if it is prime or not

Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
function is_prime($num){
	for($i=$num;$i 0;$i--){
		$v $num $i;
		if(!is_float($v)){
			if($v != $num && $v != 1)
				return FALSE;
		}
	}
	return TRUE;
}

for($i 1;$i<=100;$i++){
	if(is_prime($i))
		echo $i.' is prime<br>';
	else
		echo $i.' is not prime<br>';
}
?>

Comments

ADD YOUR COMMENT:

Formatting

Comments.
fabio July 2nd 2009
Terrifying !!!!!!!!!!!!!!!!!!!
Anonymous May 17th 2009
That's horribly inefficient...

Login



Did you want to add your own codes?
Create an Account its fast and easy!

Did you forget your username/password?
Have us reset it it for you!

Information

1.5 / 5 (8 votes)

1,587 views

1 downloads New

Code Tools

Bookmark and Share

Programmer

admin

Posted on: Apr 20, 2009

View all of Ryan's snips