Skip to content

Added Count-Primes tutorials #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions tutorials/basic-topics/Count-primes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
---
title: 'Count Primes'
description: 'Prime numbers are numbers that have only 2 factors: 1 and themselves.'
hide_table_of_contents: true
keywords:
- leetcode
- tutorial
- count primes
- Sieve of Eratosthenes
- algorithm
---

<TutorialAuthors names="@M-Rahul1"/>

## Overview
In this we are given an integer, N. The goal is to count how numbers less than N, are primes. The integer is constrained to be non-negative.

## Explanation
Primes less than 10 are 2, 3, 5 and 7. So, the count is 4.

## Approach(Brute Force)
The general approach is to check for every integer less than N and increment the result if they are prime. For example, consider N = 10. Now, we can run a check from 2 to N – 1 to find how many primes lie in this range.

### Algorithm
We can check if each number is a prime or not. To check if the number 'i' is prime we will traverse all the numbers till[2,sqrt(i)] can check if they divide N or not.
Similarly, we do this for all the N numbers.

### C++ Program
``` C++

#include <bits/stdc++.h>
using namespace std;

//Function to check whether number is prime or not

bool isPrime(int N)
{
for(int i = 2 ; i * i <= N ; i++)
if(N % i == 0)
return false;
return true;
}
//Function to count number of primes upto N
void primeSieve(int N){
int count=0;
for(int i=2;i<=n;i++){
if(isPrime(i)){
count++;
}
}
return count;
}

int main()
{
int N = 10;
cout << primeSieve(N) << '\n';
}
```
```
Output:
4
```
#### Time Complexity:
We run a loop for N/2 times. In every check, a time of complexity O(N / 2) (taking average as N / 2) is spent. So, the time complexity of this algorithm is O(N√N).

#### Auxiliary Space:
O(1), Only constant space is used for constant variables.

## Approach(Optimal Method)
#### Sieve of Eratosthenes

### Algorithm
We start from 2, and on each encounter of a prime number, we mark its multiples as composite.
### Time Complexity: O(n log log n)
### Space Complexity: O(n)

## Implementation

``` C++ Program
#include <bits/stdc++.h>
using namespace std;

int primeSieve(int N)
{
int count = 0;
int prime[N]={0};
for(int i = 2 ; i <= N ; i++)
{
if(prime[i]==0)
for(int j = i * i ; j < N ; j += i)
prime[j] = 1;
}

for(int i = 2 ; i <= N ; i++)
if(prime[i]==0)
count++;

return count;
}

int main()
{
int N = 10;
cout << primeSieve(N) ;
}
```
```
Output:
4
```

### Suggested Problems
https://leetcode.com/problems/count-primes/