(algorithm) to find prime or prime numbers between 1 and 100 million

Source: Internet
Author: User

Topic:

Number of prime or prime numbers between 1 and 100 million

Ideas:

What is prime?

Prime number, also known as prime number, has an infinite number. A natural number greater than 1, except 1 and itself, cannot be divisible by other natural numbers (prime numbers), in other words, the number has no other factor other than 1 and itself; otherwise called composite. (from Baidu Encyclopedia)

Method 1:

Iterate through all the numbers from 1 to 100 million, and then judge one by one whether it is a prime number;

How to judge a number as prime? According to the definition of prime number, it is judged whether it can be divisible by 1 and the number outside of itself, if it is, it is not prime, otherwise it is prime; it is generally only possible to determine whether the number of integers can be 2 to sqrt (num).

Method 2:

The above method has a high time complexity and is less practical for 100 million numbers.

In fact, when we traverse to a certain number (for example: N), all the numbers of n multiples are not prime, we do not need to judge this number of operations, which can reduce a lot of unnecessary calculations;

Moreover, we can consider from the inverse thinking, out of the multiple n multiples of the number, the other is prime, and thus less to determine whether the process of prime numbers.

All we need to do now is to save these numbers in advance as non-prime number, through a bool array, and if a number is a non-prime, mark the array position corresponding to the subscript for subsequent judgment.

Complexity of Time: O (n), spatial complexity: O (n)

Attention:

This is handled by 100 million numbers, so consider the memory problems of the machine, the 32-bit machine's int represents the range of 0~2^32-1 that is 4394967295, can represent 100 million, 100 million int requires 400M of memory.

(The following code is established in case of sufficient memory)

Code:
#include <iostream> #include <vector>using namespace std;const unsigned int max_num=100;void Prime (vector <bool> &numbers,vector<int> &primes) {for    (unsigned int i=2;i<=max_num;i++) {        if ( Numbers[i]==false) {            primes.push_back (i);            Multiple of I for            (unsigned int j=i;j<=max_num;j+=i)                numbers[j]=true;        }    } int main () {    vector<bool> numbers (max_num,false);    Vector<int> primes;    Prime (numbers,primes);    Vector<int>::iterator It=primes.begin ();    for (; It!=primes.end (); it++) {        cout<<*it<<endl;    }    return 0;}

(algorithm) to find prime or prime numbers between 1 and 100 million

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.