Various algorithms for finding prime numbers

Source: Internet
Author: User
Tags php language

First of all to declare my level limited, just do a record, the wrong place please correct me, the article garbage please tolerant!!


On the Internet accidentally browsing to a technical blog, called the "N realm of the prime number algorithm (N>10)", well-written, interested readers themselves to search. And then you want to try it on your own. This blog is written in various ways of seeking prime numbers.

Do not want to take the environment, the temporary use of PHP language, in Apache run, simple test.


First clarify the concept

Prime number, also known as prime number, has an infinite number. Prime numbers are defined as natural numbers greater than 1,

Except for 1 and it itself no longer has the number of other factors called prime numbers.

100 Internal Quality Tables

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

53 59 61 67 71 73 79 83 89 97

The number of prime numbers is infinite.


The opposite is composite.

Composite, mathematical terminology, English name composite number, refers to the natural numbers in addition to being 1 and the entire exception,

It can also be divisible by other numbers (except for 0) (for example: 4,6,8,9,10).

The opposite is prime, and 1 is neither prime nor composite. The smallest composite is 4.


The following is a prime number algorithm within 100

1. The most basic wording

$a = 1;//sequence number, identification number

for ($i = 2; $i < 101; $i + +) {

$primes = 0;

for ($k = 1; $k <= $i; $k + +)

if ($i% $k = = = 0) $primes + +;

if ($primes <= 2) {//Can be divided by 1 and its own integers (excluding 0)

echo $a. ". {$i}

";

$a + +;

}

}


2. Consider that all numbers can be divisible by 1 and

If there is a factorization (other than itself), it will certainly be less than or equal to $i/2, so $i/2

It is not easy to change the first algorithm's statement for ($k = 1; $k <= $i; $k + +) to

for ($k = 1; $k <= $i/2; $k + +); Why don't you try it on your own?


$a = 1;//sequence number, identification number

for ($i = 2; $i < 101; $i + +) {

$primes = 0;

for ($k = 2; $k <= $i/2; $k + +)

if ($i% $k = = = 0) $primes + +;

if ($primes <= 0) {//Can be divided by 1 and its own integers (excluding 0)

echo $a. ". {$i}

";

$a + +;

}

}


3. In addition to 2, all possible qualitative factors are odd.

So Test 2 first, and then test all the odd numbers from 3 to $I/2.

Key Special Considerations Number 2

$a = 1;//sequence number, identification number

for ($i = 2; $i < 101; $i + +) {

$primes = 0;

if ($i! = 2 && $i%2 = = = 0) $primes + +;


for ($k = 3; $k <= $i/2; $k = $k +2) if ($i% $k = = = 0) $primes + +;


if ($primes <= 0) {//Can be divided by 1 and its own integers (excluding 0)

echo $a. ". {$i}

";

$a + +;

}

}


4. In fact, as long as from 21 straight try to √x (open square), you can.

$a = 1;//sequence number, identification number

for ($i = 2; $i < 101; $i + +) {

$primes = 0;

if ($i! = 2 && $i%2 = = = 0) $primes + +;


for ($k = 3; $k <= sqrt ($i); $k = $k +2) if ($i% $k = = 0) $primes + +;


if ($primes <= 0) {//Can be divided by 1 and its own integers (excluding 0)

echo $a. ". {$i}

";

$a + +;

}

}


5. In fact, as long as from 21 straight try to √x (open Square) All of the prime numbers can be

Often mentioned in algorithmic theory: space-changing time. is to save the previous results and use them again.

The following I write only with an array to achieve this algorithm, I have limited technology, I do not know whether this accurate understanding of the original Bo

The intention of the guest writer, just take a look.

$arr =array ();

$a = 1;//sequence number, identification number

for ($i = 2; $i < 101; $i + +) {

$primes = 0;

if ($i! = 2 && $i%2 = = = 0) $primes + +;

if (!empty ($arr)) {


foreach ($arr as $key = = $value) {

if ($value <= sqrt ($i)) {

if ($i% $value = = = 0) $primes + +;

}

}

}

if ($primes <= 0) {//Can be divided by 1 and its own integers (excluding 0)

Array_push ($arr, $i);

echo $a. ". {$i}

";

$a + +;

}

}


Next, we do a simple performance test, compare the pros and cons of each algorithm, just consider the following is the test results in terms of time


Because the algorithm is not very big difference, do not compare, compare the advantages and disadvantages of the following 1,4,5

Within 100

Algorithm One

[Time:0.00099992752075195]s

Algorithm Five

[Time:0.0010001659393311]s

The results show that there is little difference within 100


Within 1000

Algorithm One

[Time:0.059004068374634]s

Algorithm Four

[Time:0.004000186920166]s

Algorithm Five

[Time:0.035001993179321]s

The results show that within 1000, the algorithm four has highlighted the advantages


Within 10000

Algorithm One

[Time:4.537260055542]s

Algorithm Four

[Time:0.19901204109192]s

Algorithm Five

[Time:1.9741129875183]s

The results show that within 10000, the algorithm is dead.

The algorithm is not five.


Within 100000

Algorithm One

[Time:542.75104403496]s

Algorithm Four

[Time:3.6972119808197]s

Algorithm Five

[Time:164.25539493561]s

The results are shown within 100000, except that the algorithm four is feasible.


Summary: Began to think that the algorithm will be more than five, do not know that I am writing garbage, or PHP array of the underlying problem, may also I do not understand the nature of space-changing time, so the current algorithm 4 is optimal.


I have limited level, just do a record, the wrong place please correct, the article garbage please tolerant!!


This article is from the "12441147" blog, please be sure to keep this source http://12451147.blog.51cto.com/12441147/1918568

Various algorithms for finding prime numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.