Important alert: (current site time 7/15/2013 10:42:39 AM EDT)
 

VB icon

A Prime Number Finder

Email
Submitted on: 1/29/2004 8:58:39 PM
By: Robert Peterson  
Level: Beginner
User Rating: By 1 Users
Compatibility: PHP 3.0, PHP 4.0
Views: 12354
 
     Finds prime numbers from 1 to whatever you choose. Has a built in time to see how long it took to find all hte primes.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: A Prime Number Finder
// Description:Finds prime numbers from 1 to whatever you choose. Has a built in time to see how long it took to find all hte primes.
// By: Robert Peterson
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1261&lngWId=8//for details.//**************************************

<?
$time_start = microtime(1);
$x = 250; // From 1 to x
for($i=1;$i<=$x;$i++) { 
$f=sqrt($i);
for($z=1;$z<=$f;$z++) { 
$y=$i%$z;
if($y==0)
$l++;
if($l>=2)
break;
}
if ($l<=1){ 
echo "$i, ";
$b++; 
}
$l=0;
}
$time_end = microtime(1);
$time = $time_end - $time_start;
echo "<br><b>[Found first $x prime numbers in $time seconds]</b>";
?>


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

2/6/2004 9:22:28 AMMartyn Merrett

Good code.

A useful addition would be to allow the user to start from a particular number as well (say from 10,000 to 20,000).

Another addition would be set_time_limit(300). As when I looked for Primes from 1 to 1,000,000 it timed out at 30 seconds (default). 300 seconds, or 5 minutes, should be long enough.
(If this comment was disrespectful, please report it.)

 
6/23/2004 10:44:56 AMJonathan Naamad

wouldnt it be much more efficient if you make it loop over the prime numbers that were already outputted to see if it is prime? (ex, to see if 11 is prime check out all numbers outputed upto sqrt(11))
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.