Leetcode 204 Count Primes (prime number Count) (*)

Source: Internet
Author: User

translation
计算小于一个非负整数n的质数的个数。
Original
thenumberofanumber, n.
Analysis

This problem encountered before, at that time is the most stupid method, and now there is no good idea, and just the topic has a hint, I opened. The tip of the topic is a one to give out, I also on one after the whole point opened, feel good failure ...

 Public int CountPrimes(intN) {intCount =0; for(inti =1; I < n; i++) {if(IsPrime (i)) count++; }returnCount;}Private Boolean IsPrime(intNUM) {if(Num <=1)return false;//Loop ' s ending condition is i * I <= num instead of i <= sqrt (num)   //To avoid repeatedly calling an expensive function sqrt ().    for(inti =2; I * i <= num; i++) {if(num% i = =0)return false; }return true;}
ofisofthetotothattheis surprisingly simple.

 Public intCountPrimes (intN) {Boolean[] IsPrime =New Boolean[n]; for(inti =2; I < n; i++) {Isprime[i] =true; }//Loop ' s ending condition is I * I < n instead of I < sqrt (n)   //To avoid repeatedly calling an expensive function sqrt ().    for(inti =2; I * i < n; i++) {if(!isprime[i])Continue; for(intj = i * I; J < N; J + = i) {Isprime[j] =false; }   }int Count=0; for(inti =2; I < n; i++) {if(Isprime[i])Count++; }return Count;}

The above are Leetcode original ...

Translate the above code into the following:

Class Solution { Public:BOOL IsPrime(intNUM) {if(Num <=1)return false; for(inti =2; I*i <= num; ++i) {if(num%i = =0)return false; }return true; }intCountPrimes (intN) {BOOL*isprime =New BOOL[n]; for(inti =2; I < n; ++i) {Isprime[i] =true; } for(inti =2; I*i < n; ++i) {if(!isprime[i])Continue; for(intj = i*i; J < N; J + = i) {Isprime[j] =false; }        }intCount =0; for(inti =2; I < n; ++i) {if(Isprime[i]) count++; }returnCount }};

Excerpt some code:

Class Solution { Public:int CountPrimes(intN) {Switch(n) { Case 0: Case 1: Case 2:return 0; Case 3:return 1; Case 4: Case 5:return 2; Case 6: Case 7:return 3; Case 8: Case 9: Case Ten: Case  One:return 4; Case  A: Case  -:return 5; Case  -: Case  the:return 6; Case 10000:return 1229; Case 499979:return 41537; Case 999983:return 78497; Case 1500000:return 114155; }    }};
intCountPrimes (intN) {if(--n <2)return 0;intM = (n +1)/2,Count= m, k, u = (sqrt (n)-1)/2; BOOL Notprime[m] = {0}; for(inti =1; I <= u;i++)if(!notprime[i]) for(k = (i+1)*2*i; K < M;k + = i*2+1)if(!notprime[k]) {Notprime[k] =true;Count--; }return Count;}

Leetcode 204 Count Primes (prime number Count) (*)

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.