Get a C + + implementation of optimization filtering method for prime numbers less than n

Source: Internet
Author: User

Correlation theorem and inference of twin primes (adjacent primes with 2 interval) P1: When n is not less than 6 and N-1 and n+1 are twin primes, then n must be a multiple of 6 T1: When n is not less than 1 and n=6x-1 or n=6x+1 is not a prime number, then n must not be 2 and 3 times Number P2: When n is not less than 5 o'clock, if n is prime, then n mod 6 = 1 or n mod 6 = 5T2: A number greater than 5 has and only divisible by 6 is 1 or 5 is possible prime number

The remainder of a number divisible by 6 may be 1,2,3,4,5 but the remainder of 2,3,4 is definitely composite

The specific code is as follows
/* *************************************************************** * NAME:select_prime.cpp * DESC: * author:l IU Dongguo ([email protected]) * version:1.0 * create:2015-04-09 13:59:45 * lutime:2015-04-10 01:53:40 * * Copyright ( c) 2007-2015 Abodu.com,inc. All Rights Reserved * *************************************************************** * *#include <stdio.h>#include <math.h>#include <string.h>Char* GETPRIMESLESSN (intNSize)//nsize is the upper value{intFlen= nSize;Char* flag=New Char[Flen];//    memset(Flag,' y ', Flen); flag[0]=flag[1]=' s ';//Exclude 0 and 1     for(intI=2;i*i<nsize;) { for(intJ=i*i;j<nsize;j+=i) {flag[j]=' n '; }//Probe The possible steps of the next prime number from the current prime        if(i==2)//2 Special: The distance to 3 is 1.{i++;Continue; }one of the//2,4,6i+=2;if(flag[i]==' n ')//2 was marked.{i+=2;if(flag[i]==' n ')//4 was marked before.i+=2; }    }returnFlag;}intMainintargcChar*argv[]) {intflen=10000;Char* FLS=GETPRIMESLESSN (Flen);intI=0, cnt=0; while(I<=flen) {if(fls[i]==' y ')//The array element with the content is ' Y ' is labeled as prime number{printf("%5d", i); cnt++;if(! (cnt% -))printf("\ n");    } i++; }printf("\n0-%d has%d prime numbers.\n", Flen, CNT);Delete(FLS);//release of memory allocated by GETPRIMESLESSN    return 0;}/ * END main * /

Get a C + + implementation of optimization filtering method for prime numbers less than n

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.