POJ 2689 Sieve method to calculate prime number

Source: Internet
Author: User

DES: Give a range [L, U]. Find the nearest two primes in this interval and the two prime numbers farthest away from each other. The length of 1<=l<u<=2147483647 interval is not more than 1000000.

Idea: Because the given u range exceeds the maximum of int. So you can't directly hit the 1-u prime table. "We know. When using the prime sieve method, all the composite are sifted away. Then we can also find a way to l-u inside the composite are filtered out. So as long as the sqrt (U) in the prime number to sift out the l-u composite can be. " just about to understand ....

1#include <stdio.h>2#include <iostream>3#include <string.h>4#include <iostream>5 using namespacestd;6 7 #defineN 5000008 #defineLen 100000009 #defineINF 0x7fffffffTen BOOLisprime[n+5]; One Long LongPrime[n], CNT; A BOOLres[len+5]; -  - voidInit () {//Filter the prime number within 50000. Be aware that I will be more prone to the range of int when it is larger the      Long LongI, J; -CNT =0; -memset (IsPrime,true,sizeof(IsPrime)); -       for(i=2; i<=n; ++i) { +         if(Isprime[i]) { -prime[cnt++] =i; +             if(I*i <=N) { A                  for(J=i*i; j<=n; j+=i) { atISPRIME[J] =false; -                 } -             } -         } -      } - } in  - intMain () { to     Long LongL, U, I, J, K, Minn, Maxx, S, T; + init (); -      while(~SCANF ("%lld%lld", &l, &U)) { thememset (Res,0,sizeof(res)); *          for(i=0; i<cnt; ++i) { $s = (l1)/prime[i]+1;//s and T represent the minimum and maximum multiples of the number pairs for this prime in the current range. For example, the interval [3, 9] for the prime number 2 S and T should be 2, 4Panax Notoginsengt = U/prime[i];//By This example can know why L-1 finally again +1. T can be removed directly.  -              for(j=s; j<=t; ++j) {//filter out the primes in the interval the                 if(j>1) {//Do not know why ==1 time also not calculate?????  +RES[J*PRIME[I]-L] =true; A                 } the             } +         } -K =-1, Minn = inf, Maxx =-1; $         Long LongDIS, m1, M2; $          for(i=0; i<=u-l; ++i) {//Solving optimal values -             if(!Res[i]) { -                 if(k!=-1) { theDis=i-k;//record the maximum distance of two primes.  -                     if(Dis >Maxx) {WuyiMaxx =dis; theM1 =i; -                     } Wu                     if(Dis <Minn) { -Minn =dis; AboutM2 =i; $                     } -                 } -                 if(I+l! =1)//Note that when you are 1, the special Judgment 1 is not prime.  -k=i; A             } +         } the         if(Maxx = =-1) -printf"there is no adjacent primes.\n"); $         Elseprintf"%lld,%lld is closest,%lld,%lld is most distant.\n", M2-minn+l, M2+l, M1-maxx+l, m1+L); the     } the     return 0; the}
View Code

The code can be understood. Naked knocks like ... It's still a bit difficult. I'll knock it again tomorrow.

POJ 2689 Sieve method to calculate prime number

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.