About the "prime Sieve Method"

Source: Internet
Author: User

First consider a proposition: if a number is not a prime, then there must be a factor less than its prime. The correctness of this proposition is obvious.

So, if I've got all the primes that are less than a single number, we just need to make sure that the number cannot be divisible by these primes, which is the prime number.

Another angle: When I get a prime number, mark all multiples of it as non-primes, so that when we traverse to a count, it is not marked as a non-prime by any primes less than it, OK

It is a prime number.

Prime

Title Description:

Enter an integer n (2<=n<=10000), which requires that all outputs from 1 to this integer (excluding 1 and this integer) be a prime number of 1 digits, and if not, output-1.

Input:

Enter more than one set of data.
For each group of rows, enter N.

Output:

Outputs all from 1 to this integer (excluding 1 and this integer) the prime number of bits 1 (the prime number is separated by a space, there is no space after the last prime number), and if not, the output-1.

Sample input:
100
Sample output:
11 31 41) 61 71


Code:
#include <cstdio>using namespacestd;voidInitprime (BOOLMark[],intN) {     for(intI=1; i<=n;++i) {Mark[i]=true; }     for(intj=2; j<=n;++j) {        if(mark[j]==false)            Continue;  for(intk=2*j;k<=n;k=k+j) {Mark[k]=false; }    }} intMain () {intN; Const intArrsize=100000; BOOLMark[arrsize];  while(SCANF ("%d", &n)! =EOF)        {initprime (mark,n); BOOLHaveprime=false; BOOLisfirst=true;  for(intI=2; i<n;++i) {            if(mark[i]==true){                if(i%Ten==1){                    if(IsFirst) {printf ("%d", i); Haveprime=true; IsFirst=false; }Else{printf ("%d", i); }                }            }        }        if(haveprime==false) printf ("-1"); printf ("\ n"); }    return 0;} /************************************************************** problem:1163 User:lcyvino language:c++ Re sult:accepted time:0 Ms memory:1044 kb****************************************************************/

Goldbach ' s conjecture

Title Description:

Goldbach ' s conjecture:for any even number n greater than or equal t o 4, there exists at least one pair of prime numbers P1 and p2 such that n = p1 + p2. 
This conjecture have not bee N proved nor refused yet. No One is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here's to write a program this reports the number of all the pairs of prime numbers satisfying the condition In the conjecture for a given even number.

A sequence of even numbers is given as input. Corresponding to all number, the program should output the number of pairs mentioned above. Notice that we is interested in the number of essentially different pairs and therefore do should not count (P1, p2) and (P2, p1) separately as and different pairs.

Input:

An integer is given on each input line. You may assume this each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.

Output:

Each of the output line should contain an integer number. No other characters should appear in the output.

Sample input:
610120
Sample output:
121


Code:
#include <cstdio>using namespacestd;Const intArrsize=1000000;BOOLPrime[arrsize];voidInitprime (intnum) {     for(intI=2; i<=num;++i) {Prime[i]=false; }     for(intI=2; i<=num;++i) {        if(prime[i]==true)            Continue;  for(intj=2*i;j<=num;j=j+i) {Prime[j]=true; }    }} intMain () {intnum;  while(SCANF ("%d", &num)! =EOF) {        if(num==0)             Break;        Initprime (num); intCnt=0;  for(intI=2; i<=num/2;++i) {            if(prime[i]==false&&prime[num-i]==false)                ++CNT; } printf ("%d\n", CNT); }    return 0;} /************************************************************** problem:1440 User:lcyvino language:c++ Re sult:accepted time:10 Ms memory:1996 kb****************************************************************/



About the "prime Sieve Method"

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.