forked from godkingjay/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This Java code provides a solution for LeetCode problem godkingjay#204,…
… which requires counting the number of prime numbers strictly less than a given integer n. The code employs the efficient Sieve of Eratosthenes algorithm to identify and count prime numbers within the specified range. It's a practical and optimized solution for counting prime numbers and can be easily integrated into other Java projects. The code is well-documented and includes sample test cases for verification. It offers a clear and concise implementation of the Sieve of Eratosthenes approach, making it a valuable resource for solving similar problems involving prime numbers in various programming scenarios.
- Loading branch information
1 parent
18fedc0
commit 68d9529
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* LeetCode 204. | ||
* Problem Statement-Given an integer n, return the number of prime numbers that are strictly less than n | ||
* | ||
* Test Cases: | ||
* | ||
* Example 1: | ||
Input: n = 10 | ||
Output: 4 | ||
Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. | ||
Example 2: | ||
Input: n = 0 | ||
Output: 0 | ||
*/ | ||
|
||
//Solution of Sieve of Eratosthenes approach | ||
class Solution { | ||
public int countPrimes(int n) { | ||
//using seive of eratosthenes | ||
boolean prime[]=new boolean[n+1];int c=0; | ||
Arrays.fill(prime,true); | ||
prime[0]=false; | ||
for(int i=2;i*i<=n;i++) | ||
{ | ||
if(prime[i]==true) | ||
{ | ||
for(int j=i*i;j<=n;j=j+i) | ||
prime[j]=false; | ||
} | ||
} | ||
for(int i=2;i<n;i++) | ||
if(prime[i]) | ||
c++; | ||
return c; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# LeetCode 204 - Count Primes | ||
|
||
## Problem Statement | ||
Given an integer `n`, return the number of prime numbers that are strictly less than `n`. | ||
|
||
### Test Cases | ||
|
||
#### Example 1 | ||
- Input: `n = 10` | ||
- Output: `4` | ||
- Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. | ||
|
||
#### Example 2 | ||
- Input: `n = 0` | ||
- Output: `0` | ||
|
||
## Solution | ||
This solution utilizes the Sieve of Eratosthenes algorithm to efficiently count the number of prime numbers less than `n`. | ||
|
||
### Pseudocode | ||
```java | ||
public int countPrimes(int n) { | ||
// Using Sieve of Eratosthenes | ||
boolean prime[] = new boolean[n + 1]; | ||
int count = 0; | ||
Arrays.fill(prime, true); | ||
prime[0] = false; | ||
for (int i = 2; i * i <= n; i++) { | ||
if (prime[i] == true) { | ||
for (int j = i * i; j <= n; j += i) | ||
prime[j] = false; | ||
} | ||
} | ||
for (int i = 2; i < n; i++) | ||
if (prime[i]) | ||
count++; | ||
return count; | ||
} |