Prime number Screening Method

Source: Internet
Author: User

The prime number is the problem that comes up frequently, judging whether a count is a prime, basically everyone will, but if it is a large range of prime, the common method for easy time-out, so you can use the template, the prime number to play the table.

1 //Number of primes within 10 million2#include <iostream>3#include <cstdio>4#include <cstring>5 using namespacestd;6 inta[10000005];7 voidSushu ()8 {9Memset (A,0,sizeof(a));//all numbers are marked as 0,0, and 1 for non-prime numbersTena[0]=1, a[1]=1;//0,1 marked as non-prime One      for(intI=2; i<=5000000; i++) A     { -         if(!A[i]) -              for(intJ=i+i; j<=10000000; J+=i)//each number consisting of prime numbers must not be primes. the             { -a[j]=1; -             } -     } +      for(intI=1; i<= +; i++) -         if(!A[i]) +printf"%d", i); A } at intMain () - { - Sushu (); -}

Prime number Screening 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.