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

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 4.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-31-2009, 07:33 PM   PM User | #1
Relish
New Coder

 
Join Date: May 2009
Location: Cincinnati
Posts: 45
Thanks: 0
Thanked 4 Times in 4 Posts
Relish is an unknown quantity at this point
PHP Prime Number Generator

Not sure how useful this is, but interesting. It will output all the prime numbers between the inputted start and end numbers.

PHP Code:
<form action="#" method="get">
  <input name="start" type="text"  />
  <input name="end" type="text" />
  <input name="submit" type="submit" value="Go!" />
</form>
 
<?php
  
for ($i $_GET['start']; $i <= $_GET['end']; $i++)
  {
    if(
$i != 1) continue;
    
$d 3;
    
$x sqrt($i);
    while (
$i $d != && $d $x$d += 2;
    if(((
$i $d == && $i != $d) * 1) == 0) echo $i.' ';
  }
?>
Relish is offline   Reply With Quote
Old 06-01-2009, 07:59 AM   PM User | #2
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,689
Thanks: 158
Thanked 2,184 Times in 2,171 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
You might be interested in Sieve of Eratosthenes algorithm
Edit: never mind... it's my mistake :-(
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 06-02-2009 at 09:34 AM..
abduraooft is offline   Reply With Quote
Old 06-01-2009, 09:12 AM   PM User | #3
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by abduraooft View Post
You might be interested in Sieve of Eratosthenes algorithm
if you look carefully you'll see that this is what op use,

best regards
oesxyl is offline   Reply With Quote
Old 06-30-2009, 10:30 AM   PM User | #4
mkrahmeh
New to the CF scene

 
Join Date: Jun 2009
Location: /home
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
mkrahmeh is an unknown quantity at this point
its useful indeed..thx

but note that your algorithm considers 1 as a prime and discards 2..
actually 1 is not a prime, and 2 is the only even prime..
mkrahmeh is offline   Reply With Quote
Old 06-30-2009, 05:29 PM   PM User | #5
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
Quote:
Originally Posted by mkrahmeh View Post
its useful indeed..thx

but note that your algorithm considers 1 as a prime and discards 2..
actually 1 is not a prime, and 2 is the only even prime..
wrong,

http://wordnetweb.princeton.edu/perl...s=prime+number

1 has no integral factors but itself and 1, therefor is prime,

best regards
oesxyl is offline   Reply With Quote
Old 07-01-2009, 08:31 AM   PM User | #6
mkrahmeh
New to the CF scene

 
Join Date: Jun 2009
Location: /home
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
mkrahmeh is an unknown quantity at this point
Quote:
Originally Posted by oesxyl View Post
wrong,

http://wordnetweb.princeton.edu/perl...s=prime+number

1 has no integral factors but itself and 1, therefor is prime,

best regards
it used to be a prime
it has been a controversial issue, but recently its more accepted to consider 1 as a non prime..
http://mathworld.wolfram.com/PrimeNumber.html
http://en.wikipedia.org/wiki/Prime_numbers#Primality_of_one

Last edited by mkrahmeh; 07-01-2009 at 08:43 AM..
mkrahmeh is offline   Reply With Quote
Old 09-15-2010, 04:16 PM   PM User | #7
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,230
Thanks: 11
Thanked 156 Times in 156 Posts
DrDOS is infamous around these parts
I've actually written a script to do this, but in javascript of all things. It builds the Ulam Square of prime numbers and presents it on a web page. Here it is on my testing site.

http://ronbeau.50webs.com/ulam.html

And there is also a page for a 201 x 201 square.

But there are several things you need to know about generating primes if you want to be efficient. How that page works is by declaring a array and populating it with primes and then using the data from the array to build the page. But building the array involves two steps. You only need to test with primes that are less than the square root of a given number, so you need to build a smaller array containing the numbers to check against.

You can't start your arrays with one, or every number will be considered divisible and none pushed into the array. You have to start your divisor array with two and the big array with three.

It would be very easy to translate that code into PHP but in this case that sort of defeats the purpose, since the page you download is only about 3kb but the page built is over 100kb, making more work for the server while the client machine is sitting there just wating.
DrDOS is online now   Reply With Quote
Old 07-02-2012, 04:33 PM   PM User | #8
tony_day
New to the CF scene

 
Join Date: Jul 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
tony_day is an unknown quantity at this point
<?php
$no=$_POST['num'];
for($i=2;$i<=$no-$i;$i+1){
if($no%$i==0){
echo "Not ";
break;

}
}
echo "Prime";
?>
tony_day is offline   Reply With Quote
Reply

Bookmarks

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 04:32 PM.


Advertisement
Log in to turn off these ads.