poj2909 | | poj2262

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>#include<math.h>intIspri (intAintb) {    inti;  for(i=2; I <= sqrt (a); i++)    {        if(a%i = =0)            return 0; }     for(i=2; I <= sqrt (b); i++)    {        if(b%i = =0)            return 0; }    return 1;}intMain () {intn,flag,i,cout;  while(SCANF ("%d", &n)! = EOF &&N) {cout=0;  for(i=2; i< n/2+1; i++) {flag=ispri (i,n-i); if(Flag = =1) cout++; } printf ("%d\n", cout); }    return 0;}
View Code

We use the Eratosthenes sieve method to play the table

Algorithm Description: Less than 2 is not a prime, with an array of a[10000], the element subscript indicates the number, the value of the element is the prime, C + + practice is, bool a[10000], will a[0] and A[1] assigned 0, starting from 2, 2*2,2*3 ...

2*6. These subscript corresponding element values are changed to 0, in this environment again looking for the next prime, once again, 3*2,3*3,3*4 ... product overall less than 10000

    inta[10000]; a[0]=0; a[1]=0;  for(i=2;i<10000; i++) A[i]=1;  for(i=2;i<10000; i++)    {        if(A[i] = =1)        {             for(j=2;j<10000; j + +) {A[i*J] = =0; }        }    } for(intj=2; j<=n;++j) {          if(isprimes[j]==true)               for(intm=2; j*m<=n;++m) Isprimes[j*m] =false;
View Code

See:

http://blog.csdn.net/kingwolfofsky/article/details/7199758

poj2909 | | poj2262

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.