Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-24-2010, 01:50 AM   PM User | #1
person5
New to the CF scene

 
Join Date: Apr 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
person5 is an unknown quantity at this point
Post php find a prime

PHP Code:
<?php
$number 
= (a number you want to check);
$i=2;
While(
$i $number ){
If( 
$number $i ){
echo 
"nope";
}
else
{
$i++;
}
}
?>
might not work i didn't double check it but just don't change the modulus sign %
person5 is offline   Reply With Quote
Old 10-07-2011, 04:10 PM   PM User | #2
Cjwinnit
New Coder

 
Join Date: Oct 2011
Posts: 12
Thanks: 0
Thanked 1 Time in 1 Post
Cjwinnit is an unknown quantity at this point
Hope I don't get into too much trouble but I spotted a very quick way to speed up this code..

You only need to check up to the square root of the number in question, not half the value. Example, if you were checking a number that is roughly a million, that requires your script to do up to 500,000 increments. In reality it only needs to do about 1,000.

Does PHP have a square root function?

Last edited by Cjwinnit; 10-07-2011 at 04:11 PM.. Reason: sqrt()?
Cjwinnit is offline   Reply With Quote
Old 10-07-2011, 07:54 PM   PM User | #3
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,602
Thanks: 2
Thanked 398 Times in 391 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
Quote:
Originally Posted by Cjwinnit View Post
Does PHP have a square root function?
Yeah, sqrt().
Inigoesdr is offline   Reply With Quote
Reply

Bookmarks

Tags
find, modulus, php, prime, prime numbers

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 09:53 PM.


Advertisement
Log in to turn off these ads.