Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Check for prime numbers

The IsPrime() function uses the recursive method to check if a number is prime. It's a fast function which can compute thousands of numbers within seconds.

On Wednesday, September 26th 2007 at 11:39 PM
By Andrew Pociu (View Profile)
****-   (Rated 3.8 with 13 votes)
Contextual Ads
More PHP Resources
Advertisement
  1. <?php
  2. // Checks for prime numbers
  3. function IsPrime($Num)
  4. {
  5.         $No = 0;
  6.         for($CurrNum = 2; $CurrNum <= $Num; $CurrNum++)
  7.         {
  8.                 for($Divisor = 2; $Divisor < $CurrNum; $Divisor++)
  9.                 {
  10.                         $Res = $CurrNum / $Divisor;
  11.                         if($Res != 1 && intval($Res) == $Res)
  12.                         {
  13.                                 $No = 1;
  14.                                 $Divisor = $CurrNum;
  15.                         }
  16.                 }
  17.                 if($No != 1)
  18.                 {
  19.                         $Result = $CurrNum;
  20.                 }
  21.                 $No = 0;
  22.         }
  23.  
  24.         // If the only divisor is the number itself, it's prime
  25.         if($Result == $Num)
  26.         {
  27.                 return 1;
  28.         }
  29.         else
  30.         {
  31.                 return 0;
  32.         }
  33. }
  34.  
  35. // Check for primes, 0 to 100
  36. for($i = 0; $i < 100; $i++)
  37. {
  38.         echo $i." is a prime number? ".IsPrime($i)."<br />";
  39. }
  40. ?>
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by Niko on Sunday, October 19th 2008 at 04:20 AM

The IsPrime() function doesnt only check if $Num is a Prime it is checks every number below $Num too and disregarding te results. The double loop is not needed. Also you dont have to check for divisors above the square root of $Num. Also if you check if $Num is even (returning false unless it is 2 itself) you dont have to check for any even divisors. Actuelly you only need to check with divisors that are prime numbers. But to check for those prime numbers will probably be a longer job itself. A quick middle thing could be

by Niko on Sunday, October 19th 2008 at 04:24 AM

Oops My function was cut off. I'll retry:

by Niko on Sunday, October 19th 2008 at 04:26 AM

by Garciat on Thursday, January 8th 2009 at 04:58 PM

This would be my function:

by Morten Petersen on Monday, May 11th 2009 at 06:41 PM

try this

function is_prime($num){
for ($i=2; $i

by leela on Thursday, July 23rd 2009 at 04:19 AM

how to run this prime number calculation program using php

by aamir on Sunday, August 9th 2009 at 01:46 PM

Good tutorial.i like it.

by pranipat on Wednesday, September 16th 2009 at 06:56 AM

by countcause on Wednesday, December 23rd 2009 at 10:14 PM

Testing primality in this manner becomes prohibitively expensive as the numbers you're testing increase in size. A more efficient test would be the Miller-Rabin test as implemented in phpSecLib:

http://phpseclib.sourceforge.net/

by countcause on Wednesday, December 23rd 2009 at 10:15 PM

Testing primality in this manner becomes prohibitively expensive as the numbers you're testing increase in size. A more efficient test would be the Miller-Rabin test as implemented in phpSecLib:

http://phpseclib.sourceforge.net/

by asdasasd on Sunday, April 11th 2010 at 12:27 PM

Andrew Pociu, what I am going to write here is serious.

You are an idiot. I looked at your profile and you are calling yourself a PHP expert there. This prime checking function above is one of the worst I've ever seen.
Not even my son who is 11 would code such a piece of crap. I don't even know why this is so high-voted.

by Bas on Wednesday, June 9th 2010 at 12:55 PM

All prime numbers above 5 must end with an 1, 3, 7 or 9. This makes it so you only have to check 40% of the values!

function isPrime($n)
{
if ($n > 5)
{
if (! in_array(substr($n, -1), array(1, 3, 7, 9))) // quick win
return false;

for ($i=2; $i

by neha on Monday, June 14th 2010 at 02:11 AM

Logics are not clear..comments should be written in proper way.. variables should be clearly defined

by neha on Monday, June 14th 2010 at 02:11 AM

Logics are not clear..comments should be written in proper way.. variables should be clearly defined

by Arun on Thursday, July 29th 2010 at 07:51 AM

There's a fairly straightforward, simple to understand function here -> http://daemoncoder.com/php/php-prime-number-function-script

by as on Sunday, October 10th 2010 at 04:15 PM

really
this is stupid.
have you ever thought for optimization?
?
?

by as on Sunday, October 10th 2010 at 04:15 PM

really
this is stupid.
have you ever thought for optimization?
?
?

by Lev on Thursday, October 14th 2010 at 04:43 AM

The code by Bas is a good approach, but it has a bug which will can cause an infinite loop and prevent your code from ever responding.

The problem is in the loop, as $i is never incremented; the code will just site in that loop indefinitely since $i doesn't increase and the statement will always be true.

It should look like this:

function isPrime($n)
{
if ($n > 5)
{
if (! in_array(substr($n, -1), array(1, 3, 7, 9))) // quick win
return false;

for ($i=2; $i

by Lev on Thursday, October 14th 2010 at 04:46 AM

Apparently there bug isn't/wasn't in the code Bas posted, but in this site's blasted commenting mechanism. It is stripping out the two plus symbols after $i in the for loop.

Be very careful and make sure you follow your intuition there and include two plus signs after the $i - I can't write the code as it should be because this site is hella retarded... >_

by q on Friday, October 15th 2010 at 09:17 AM

you can right i=i 1

by vav neuman on Friday, November 12th 2010 at 01:22 AM

you are stupid ,bakwas tution
dub ke mar ja

by _scorpio_ on Friday, February 18th 2011 at 04:37 PM

by jancarl on Friday, July 29th 2011 at 02:27 AM

Pls help me on my midterm, i need a program that would generate a grading system for college students...

by professional seo services on Monday, December 12th 2011 at 05:42 AM

Put everything in a method, and in the catch block you can recall the method (you can say the method calls itself). Use System.Windows.Forms.Cursor.Position to get the cursor position in screen coordinates.I admire the important information you offer within your content. I'll bookmark your web site and have my kids examine up the following typically.

by Felicia on Tuesday, January 17th 2012 at 12:41 AM

Once you have recreated the problem and captured these steps, you can save them to a file and send it to your support person, who can then open it up and view

by bestech dharuhera on Thursday, March 22nd 2012 at 04:25 AM


Very good article! This is a highly beneficial blogging site which you have.

by RanaMutarrif on Friday, August 3rd 2012 at 10:58 PM

by RanaMutarrif on Friday, August 3rd 2012 at 10:58 PM

by Rajkumar on Tuesday, September 4th 2012 at 11:09 PM

by Acne Treatment Reviews on Thursday, September 6th 2012 at 07:35 AM

You have really interesting blog, keep up posting such informative posts!

by Best Acne Treatment on Thursday, September 6th 2012 at 08:03 AM

You have really interesting blog, keep up posting such informative posts!

by Acne Scar Treatment on Thursday, September 6th 2012 at 08:22 AM

You have really interesting blog, keep up posting such informative posts!

by Hemocyl on Thursday, September 6th 2012 at 10:16 AM

You have really interesting blog, keep up posting such informative posts!

by Teeth Whitening Reviews on Thursday, September 6th 2012 at 10:56 AM

You have really interesting blog, keep up posting such informative posts!

by Diet Pills on Thursday, September 6th 2012 at 11:24 AM

You have really interesting blog, keep up posting such informative posts!

by Best Diet Pills on Thursday, September 6th 2012 at 01:06 PM

You have really interesting blog, keep up posting such informative posts!

by webex test on Tuesday, October 2nd 2012 at 02:44 AM

We want all the tables and all the columns therefore we need to take the following steps.

by freeze dried food storage life on Thursday, October 25th 2012 at 01:19 AM

the columns therefore we need to take the following steps.

by mres on Thursday, November 1st 2012 at 05:20 AM

I'll bookmark your web site and have my kids examine up the following typically.

by electric dog fence wireless on Saturday, November 3rd 2012 at 06:15 AM

I want to know more about how to use it for a website.

by invisible fence collar blinking red on Monday, November 5th 2012 at 12:49 AM

web site and have my kids examine up the following typically.

by lawkanat on Wednesday, November 7th 2012 at 12:39 AM

may i know, why you all don't put any increment in for looping.

by lawkanat on Wednesday, November 7th 2012 at 12:39 AM

may i know, why you all don't put any increment in for looping.

by B Expart on Thursday, December 6th 2012 at 11:12 AM

Thanks so much for this wonderful website as well as this post. This post is the kind of thing that keeps me on track through. I’ve been looking around for this site after I heard about them from a buddy and was thrilled. Thanks a bunch.
over here

by saiful on Friday, August 29th 2014 at 02:31 PM

Thanks..


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs PHP Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources