From 4e108e1f430c5b24e76ca7fa56cf4d37b3b4653b Mon Sep 17 00:00:00 2001 From: Daksh Kesarwani <90037965+InnocentDaksh63@users.noreply.github.com> Date: Sat, 8 Oct 2022 08:50:05 +0530 Subject: [PATCH] Create 204_count_primes.java --- 204_count_primes.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 204_count_primes.java diff --git a/204_count_primes.java b/204_count_primes.java new file mode 100644 index 0000000..4f557b7 --- /dev/null +++ b/204_count_primes.java @@ -0,0 +1,23 @@ +class Solution{ +public int countPrimes(int n) { + if(n <=1 ) return 0; + + boolean[] notPrime = new boolean[n]; + notPrime[0] = true; + notPrime[1] = true; + + for(int i = 2; i < Math.sqrt(n); i++){ + if(!notPrime[i]){ + for(int j = 2; j*i < n; j++){ + notPrime[i*j] = true; + } + } + } + + int count = 0; + for(int i = 2; i< notPrime.length; i++){ + if(!notPrime[i]) count++; + } + return count; +} +}