[Algorithm] calculates the prime number by the common method and the limit method, and the algorithm returns the prime number by the limit method.

Source: Internet
Author: User

[Algorithm] calculates the prime number by the common method and the limit method, and the algorithm returns the prime number by the limit method.

Prime Number refers to the number of factors with only 1 and itself (1 is not a prime number). It is widely used in mathematics to solve prime numbers. It is also a common problem for programming to solve prime numbers within n, in this case, the embedding method runs very fast to solve the prime number. The following describes how to determine whether a prime number is used, then describes how to calculate the prime number within n by using the common method, followed by the prime number within n by the embedding method, and finally the running time comparison of the two algorithms.

Determines whether a number is a prime number.

Algorithm idea: judge whether all integers greater than 1 that are less than or equal to a number can divide this number. If yes, it indicates that this number is not a prime number; otherwise, it is a prime number.

// Determine whether a number is a prime number, bool isPlain (int value) {int m = sqrt (value); if (value <2) return false; for (int I = 2; I <= m; I ++) {if (value % I) = 0) {return false ;}} return true ;}
The common method is to solve the prime number within n.

Algorithm idea: declare an n-sized bool array. The initial value is false. Then, start from 2 to determine whether a number is a prime number. If yes, set its Boolean value to true.

// Calculate the prime number bool * putong (int n) {bool * value = new bool [n]; for (int I = 0; I <n; I ++) value [I] = false; for (int I = 2; I <n; I ++) {if (isPlain (I) {value [I] = true ;}} return value ;}
Evaluate the limit method to obtain the prime number within n

Algorithm idea: Find the prime number of the square that is less than or equal to n, and then remove all the multiples of these prime numbers in n. The remaining number is a prime number, which is also implemented through a Boolean array.

// Evaluate the prime number bool * shuaixuan (int n) {bool * value = new bool [n]; for (int I = 0; I <n; I ++) value [I] = true; value [0] = false; value [1] = false; for (int I = 2; I <= sqrt (n); I ++) {if (value [I] & isPlain (I) {int c = 2; int j = I * c; while (j <n) {value [j] = false; j = I * c ++ ;}} return value ;}
Complete code and running results

The following code calls the common method and the explain method respectively. You can enter n cyclically (press Ctrl + C to end) for testing different data. A test result diagram is provided below.

# Include <iostream> using namespace std; # include <ctime> # include <math. h> # include <conio. h> // determine whether a number is a prime number, bool isPlain (int value) {int m = sqrt (value); if (value <2) return false; for (int I = 2; I <= m; I ++) {if (value % I) = 0) {return false ;}} return true ;} // calculate the prime number bool * putong (int n) {bool * value = new bool [n]; for (int I = 0; I <n; I ++) value [I] = false; for (int I = 2; I <n; I ++) {if (isPlain (I) {value [I] = true ;}} return value;} // calculate the prime number bool * shuaixuan (int n) {bool * value = new bool [n]; for (int I = 0; I <n; I ++) value [I] = true; value [0] = false; value [1] = false; for (int I = 2; I <= sqrt (n ); I ++) {if (value [I] & isPlain (I) {int c = 2; int j = I * c; while (j <n) {value [j] = false; j = I * c ++ ;}}return value ;}int main () {int n; while (cin >> n) {int start = clock (); bool * value1 = putong (n); int end = clock (); cout <"common method: "<end-start <endl; start = clock (); bool * value2 = shuaixuan (n); end = clock (); cout <" condition method: "<end-start <endl; delete [] value1; value1 = NULL; delete [] value2; value2 = NULL;} _ getch ();}


It can be seen that the larger n, the better the performance of the explain Method for common methods.

Related Article

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.