"204-count Primes (Statistical prime)"
"leetcode-Interview algorithm classic-java Implementation" "All topics Directory Index"
code Download "Https://github.com/Wang-Jun-Chao"
Original Question
Description:
Count the number of prime numbers less than a non-negative number, N.
Main Topic
Counts the number of primes less than the nonnegative integer n.
Thinking of solving problems
Use See Erato sieve method.
Code Implementation
Algorithm implementation class
Public class solution { Public int CountPrimes(intN) {if(N <=1) {return 0; }//default all element values will be set to False Boolean[] Notprime =New Boolean[n]; notprime[0] =true; notprime[1] =true; for(inti =2; I * i < n; i++) {//If I is a prime number, I set the multiple of I to non-prime //If I is a composite, it must have been set to true since it was processed from 2 if(!notprime[i]) { for(intj =2I J < N; J + = i) {Notprime[j] =true; } } }//Count number of prime numbers intresult =0; for(BooleanB:notprime) {if(!B) {result++; } }returnResult }}
Evaluation Results
Click on the picture, the mouse does not release, drag a position, release after the new window to view the full picture.
Special Instructions
Welcome reprint, Reprint please indicate the source "http://blog.csdn.net/derrantcm/article/details/48021413"
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Leetcode-Interview algorithm classic-java Implementation" "204-count Primes (statistical prime)"