Find the prime number within 1000000

Source: Internet
Author: User
Tags square root

Article Author: Tyan
Blog: noahsnail.com |  CSDN | Jane book 1. Primes

Prime numbers (Prime number), also known as prime numbers, refer to the number of natural numbers greater than 1, except for 1 and the number itself, that cannot be divisible by other natural numbers (which can also be defined as the number of only 1 and two positive factors of the number itself). Natural numbers greater than 1 are called composite numbers if they are not prime. 2. The method of calculating the prime number within 1000000 times the calendar

#include <iostream>
#include <cstdio>
#include <time.h>
#define NUM 1000000
using namespace Std;
int main () {
    int i, J;
    clock_t start, end;
    Double duration;
    start = Clock ();
    for (i = 2; I <= NUM. i++) {for
        (j = 2; J < I; J + +) {
            if (i% J = = 0) {break
                ;
            }
        }
        if (j = = i) {
            printf ("%d\n", I);
        }
    }
    end = Clock ();
    Duration = (double) (end-start)/clocks_per_sec;
    printf ("%f seconds.\n", duration);
    return 0;
}

Analysis: The above method is the easiest to think about, but at the same time the efficiency is lowest. Its run time: 133.186849 seconds. Method Two is improved on the basis of the above

#include <iostream>
#include <cstdio>
#include <time.h>
#define NUM 1000000
using namespace Std;
int main () {
    int i, J;
    clock_t start, end;
    Double duration;
    start = Clock ();
    printf ("%d\n", 2);
    for (i = 3; I <= NUM. i = 2) {for
        (j = 3; J < I; J + + 2) {
            if (i% j = 0 | | J * j > I) {
                break;
  }
        }
        if (J * J > I) {
            printf ("%d\n", I);
        }
    }
    end = Clock ();
    Duration = (double) (end-start)/clocks_per_sec;
    printf ("%f seconds.\n", duration);
    return 0;
}

Analysis: On the basis of the above, first of all, we can determine that the even number except 2 can be ruled out, and if the square root (adjacent integer) that executes to a number cannot be divisible by it, then the numbers cannot be divisible by them, and if divisible, the other factor must be in the number before the square root. Its run time: 0.194340 seconds. Method Three Sieve method

#include <iostream>
#include <cstdio>
#include <time.h>
#define NUM 1000000
using namespace Std;

BOOL Is_prime[num + 1];
int main () {
    clock_t start, end;
    Double duration;
    start = Clock ();
    for (int m = 2; m < NUM; m++) {
        is_prime[m] = true;
    }
    for (int i = 2; I <= NUM. i++) {
        if (is_prime[i)) {for
            (int j = 2 * i; J < NUM; j = i) {
                is_prime[j] = False
    ;
    }}} for (int i = 2; I <= NUM; i++) {
        if (Is_prime[i]) {
            printf ("%d\n", I);
        }
    }
    end = Clock ();
    Duration = (double) (end-start)/clocks_per_sec;
    printf ("%f seconds.\n", duration);
    return 0;
}

Analysis: Sieve method is to assume that all numbers are prime, and then traversal, if it is a prime number, then its multiples are number, traversing all the numbers can be. Its run time: 0.021246 seconds.

Summary: from the above running time can be seen, different methods of running time difference is very large, the code should pay attention to optimization. reference programming and Algorithms (I.)

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.