Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Apr 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 %

  • #2
    New Coder
    Join Date
    Oct 2011
    Posts
    12
    Thanks
    0
    Thanked 1 Time in 1 Post
    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()?

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by Cjwinnit View Post
    Does PHP have a square root function?
    Yeah, sqrt().


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •