Multi
Multi
-
Topic Started: Friday, 30. March 2012, 00:40 (1,203 Views)
Robot
No Avatar
Level 9
-
Off
Profile
Quote
Top
 
Culbertson789
No Avatar
Level 1
thanks for this code. i add this
Off
Profile
Quote
Top
 
Andrew
Member Avatar
ぼくたちがすべてはばか。
Of course you could always just use a loop to loop through every odd number below the number to check for a prime number or not.

Code: PHP
 
<?php
function prime($num){
//if the number is not even and is greater than 0.
if($num % 2 != 0 && $num > 2){
//For each number until n > num.
//Increment by 2 for efficiency since we checked for even numbers.
for($n = 3; $n < $num; $n += 2)
//If the number modulus another number is equal to 0.
if($num % $n == 0)
return false;

//If it gets here then the number is prime.
return true;
}
//If the number is even or less than 0.
else
return false;
}

echo prime(10) == true ? "Prime" : "Not Prime";
?>p
Posted Image

Professional web design/development services.http://wildandrewlee.com/
Off
Profile
Quote
Top
 
Robot
No Avatar
Level 9
-
Off
Profile
Quote
Top
 
Andrew
Member Avatar
ぼくたちがすべてはばか。
Flavius
Friday, 6. April 2012, 23:14
Pro
Friday, 6. April 2012, 22:42
Of course you could always just use a loop to loop through every odd number below the number to check for a prime number or not.

Code: PHP
 
<?php
function prime($num){
//if the number is not even and is greater than 0.
if($num % 2 != 0 && $num > 2){
//For each number until n > num.
//Increment by 2 for efficiency since we checked for even numbers.
for($n = 3; $n < $num; $n += 2)
//If the number modulus another number is equal to 0.
if($num % $n == 0)
return false;

//If it gets here then the number is prime.
return true;
}
//If the number is even or less than 0.
else
return false;
}

echo prime(10) == true ? "Prime" : "Not Prime";
?>p
Oh! That's an awesome way to do it. Really great. Only thing is, there's no check for square numbers. Try 121.
Don't worry :P that code accounts for everything including 121.
Posted Image

Professional web design/development services.http://wildandrewlee.com/
Off
Profile
Quote
Top
 
IllegallySniped
Member Avatar
Island-Z v0.0.08
Awesome!
I wish I could code like this ( -.-)
Posted Image
Forum currently under construction!
Off
Profile
Quote
Top
 
Robot
No Avatar
Level 9
-
Off
Profile
Quote
Top
 
Andrew
Member Avatar
ぼくたちがすべてはばか。
It pretty much increments through the odd numbers only because if the number would be divisible by an even number it isn't prime which is why I check if the number is an even number first to halve the number of checks. After that I check each odd number below the number to see if the number mod the number being used to check is 0. If so then it isn't odd otherwise keep checking other numbers.
Posted Image

Professional web design/development services.http://wildandrewlee.com/
Off
Profile
Quote
Top
 
Robot
No Avatar
Level 9
-
Off
Profile
Quote
Top
 
Andrew
Member Avatar
ぼくたちがすべてはばか。
Yeah :P I always forget about that up to half the number part :P
Posted Image

Professional web design/development services.http://wildandrewlee.com/
Off
Profile
Quote
Top
 
Quozzo
Member Avatar
Level 3
Sorry for necro-ing this old post, but i wrote a code in JavaScript that would attempt to divide the number by every integer, starting at 2, and if there was no remainder for all the intergers up to that number then it was not a prime. But a better way of detecting a prime number is dividing it by all the previous prime numbers, because if a number isn't a prime number then it obviously is divisible by a prime number.

I'm only telling you because in JS if a number was a prime i stored it in an array (of primes) and that was my list of numbers to divide each number against, but for some reason it was way slower than just basically dividing it by every number incrementing by one each time, even though i basically omitted 90% of the number of checks. I guess it had something to do with the time it took to check the index of the array etc.

My point being is that perhaps the slow way for JS could in fact be better for PHP as it really does reduce the number of checks dramatically.

Here's my JS if your interested.
http://s4.zetaboards.com/quozzostestboard/pages/prime/
Edited by Quozzo, Sunday, 9. September 2012, 21:45.
You're unique, just like everyone else.
Off
Profile
Quote
Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Coding & Development · Next Topic »

Welcome Guest [Log In] [Register]
Outline Live
Loading..
Loading..