Evaluate the prime number within N (definition of prime number: in a natural number greater than 1, apart from 1 and itself, it cannot be divisible by other natural numbers)

Source: Internet
Author: User

Idea: 1. (prime number filtering theorem) n cannot be divisible by any prime number not greater than root number N, then n is a prime number.
2. Even numbers except 2 are not prime numbers.
The Code is as follows:

/*** Calculate the prime number in N * @ Param int $ N * @ return array */function get_prime ($ n) {$ prime = array (2 ); // 2 is the prime number for ($ I = 3; $ I <= $ N; $ I + = 2) {// the even number is not the prime number, increase the step size by $ SQRT = intval (SQRT ($ I); // obtain the root number N for ($ J = 3; $ j <= $ SQRT; $ J + = 2) {// I is an odd number. Of course, it cannot be divisible by an even number. The step size can also be increased. If ($ I % $ J = 0) {break;} if ($ j> $ SQRT) {array_push ($ Prime, $ I );}} return $ prime;} print_r (getprime (1000 ));

Evaluate the prime number within N (definition of prime number: in a natural number greater than 1, apart from 1 and itself, it cannot be divisible by other natural 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.