Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 10-23-2010, 01:21 PM function is_prime
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
I made this function as a test to see whether a number is prime or not. Example usage is at the bottom.

PHP Code:
function is_prime($n) {
  
$r range(1$n);
  
$nA = array();
  foreach (
$r as $s) {
   
$p $n/$s;
   if (
is_int($p)==true) {
     if (
strpos($p,".")==false) {
       
array_push($nA,$p);
     }
     else {
$nA=$p;}
   }
 }
  if (
count($nA)==2) {return true;}
  else {return 
false;}
}
/*use is_prime(number to check).  If the number is prime, it will return true, if it's false, it will return false.*/
if (is_prime(5)==true) {echo "true";}else {echo "false";} 
Hope you find this helpful
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-23-2010, 10:10 PM Re: function is_prime
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Isn't calling is_int and checking if the number contains a decimal redundant?

A function like that would be acceptable for small numbers, but once you start checking if very large numbers are prime its going to slow down considerably.

One thing you can do to speed it up is only check for prime factors; ie once you've determined that number is not divisible by 3 you know its not divisible by 6, 9, 12 etc.

For very large numbers you may want to look into better methods:
http://en.wikipedia.org/wiki/Primality_test

Edit:
I forgot to mention. If you want to test if a number is evenly divisible by another number use modulus (%). If x % y is 0 then y is a factor of x.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 10-23-2010 at 11:13 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 10-24-2010, 08:32 AM Re: function is_prime
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Mmm.

And thanks NullPointer, this function was simply designed for small numbers, not huge ones anyway :P
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to function is_prime
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.11700 seconds with 12 queries