Codevs 2530 Large prime numbers

Source: Internet
Author: User

Links: http://codevs.cn/problem/1530/

Problem Solving Ideas:
The most critical pruning of the problem is that the factor is less than the square root, but it is not used as originally.
Reversing thinking is more about judging which is not prime, simpler and more efficient than judging which is prime.
All composite have a common feature, which is that they can be split into mass factors.
Then the quality factor that has appeared, sooner or later, will become a composite factor.
Then directly take the number to judge by the prime numbers in addition to the first time, you can directly judge whether it is composite.
Write this, the first three points on the past, as for the last 100000, is the beginning of the pruning.
In any case, the quality factor within a composite must be less than its square root.
So in advance to judge, try to the first few composite stop, you can greatly improve efficiency.

1#include <cstdio>2#include <iostream>3#include <cmath>4 using namespacestd;5 intn,ans,prime[100001],PH;//the ph indicates the number of prime numbers currently being searched. 6 7 intDivideDoublex) {//Judging the number of prime numbers to be tested8     intTemp= (int) x;9      for(intI=1; i<ph;i++){Ten         if(prime[i]>temp)returnI-1; One     } A } -  - intMain () { theCin>>N; -prime[1]=2; -prime[2]=3; -Ph=2; +     BOOLcheck; -     inttemp,test; +      while(ph<n) {//Recursive Atemp=prime[ph]; atph++; -          while(1){ -temp++; -test=Divide (sqrt (temp)); -              for(intI=1; i<=test;i++) { -                 if(Temp% prime[i]==0) {check=true; Break;} in}//I 'll do it again. -             if(check) {check=false;Continue;} toprime[ph]=temp; +              Break; -         } the     } *cout<<Prime[n]; $}

Codevs 2530 Large prime numbers

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.