"Leetcode-Interview algorithm classic-java Implementation" "204-count Primes (statistical prime)"

Source: Internet
Author: User

"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)"

Related Article

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.