L6. Sieve of Eratosthenes | Maths Playlist

take U forward
17 Mar 202418:26

Summary

TLDRIn this video, the instructor explains an optimized algorithm for finding prime numbers up to a given number using the Sieve of Eratosthenes. The initial brute force method is discussed, followed by the introduction of the sieve algorithm, which significantly reduces time complexity by precomputing prime numbers. The video covers the step-by-step process, explains the logic behind eliminating non-prime numbers, and introduces optimizations to further enhance efficiency. Finally, the video provides insights into the time and space complexities of the algorithm, offering helpful code implementation tips.

Takeaways

  • 🧮 The problem involves finding all prime numbers up to a given number, 'n'.
  • ⚙️ The brute force method has a time complexity of O(n * √n), which is inefficient for large values of 'n'.
  • ⏱️ The optimized solution uses the Sieve of Eratosthenes algorithm, which improves time complexity by precomputing prime numbers.
  • 🔢 The key idea is that for any prime number, all its multiples are not prime and can be marked as non-prime.
  • 🟢 The algorithm works by creating an array of size 'n+1', marking 1 for prime candidates and 0 for non-prime numbers.
  • ✖️ Once a prime is identified, all its multiples are marked as non-prime starting from the prime squared.
  • 🚀 The optimized time complexity of the algorithm is O(n log log n), thanks to precomputation and the efficient marking process.
  • 📝 The sieve only needs to loop up to the square root of 'n', reducing unnecessary computations for larger values.
  • 🧠 The space complexity is O(n) because of the additional array used to store the prime status of each number.
  • 📈 This approach is highly efficient for large values of 'n', and the precomputation step is the key to optimizing prime number checking.

Q & A

  • What is the problem the speaker is trying to solve?

    -The problem is to print all prime numbers up to a given number 'n'. For example, if n = 10, the prime numbers up to 10 are 2, 3, 5, and 7.

  • What is the brute force solution mentioned for finding prime numbers up to n?

    -The brute force solution involves iterating through all numbers from 2 to n and checking each number to see if it's prime by dividing it by all numbers up to its square root. This method has a time complexity of O(n√n).

  • Why is the brute force solution inefficient for large values of n?

    -The brute force solution is inefficient because for large values like n = 10^5 or 10^6, the time complexity grows significantly, leading to an enormous number of operations, making the algorithm slow.

  • What is the optimized approach introduced in the script?

    -The optimized approach introduced is the Sieve of Eratosthenes, an algorithm that marks the multiples of each prime number as non-prime. This allows prime numbers to be determined in constant time after some initial precomputation.

  • How does the Sieve of Eratosthenes algorithm work?

    -The algorithm works by creating an array where all numbers are initially marked as prime. Starting from the first prime (2), it marks all multiples of 2 as non-prime, then moves to the next number (3), marking all its multiples, and so on. This process continues until all numbers are either marked as prime or non-prime.

  • What is the time complexity of the Sieve of Eratosthenes?

    -The time complexity of the Sieve of Eratosthenes is O(n log log n), which is much more efficient than the brute force method.

  • How does the Sieve of Eratosthenes avoid unnecessary computations?

    -The algorithm avoids unnecessary computations by starting from the square of each prime number when marking multiples, as smaller multiples would have already been marked by earlier primes. Additionally, it only processes numbers up to the square root of n, as larger numbers are already covered.

  • What space complexity does the Sieve of Eratosthenes have?

    -The space complexity of the Sieve of Eratosthenes is O(n), as it requires an array of size n to store whether each number is prime or not.

  • What optimizations are suggested for the Sieve of Eratosthenes?

    -The two main optimizations are: 1) starting to mark multiples from the square of each prime number to avoid redundant work, and 2) stopping the outer loop once it reaches the square root of n, as no further marking is necessary beyond that point.

  • What is the role of precomputation in the Sieve of Eratosthenes?

    -Precomputation plays a critical role in the Sieve of Eratosthenes by calculating which numbers are prime in advance. Once this precomputation is done, determining whether any given number is prime can be done in constant time, O(1).

Outlines

00:00

🔢 Introduction to Prime Number Algorithm and Basic Brute Force Approach

In this section, the speaker introduces a problem related to prime numbers, explaining the task of finding all prime numbers up to a given number n. The example provided uses n = 10, where the prime numbers are 2, 3, 5, and 7. The speaker presents a brute force solution where, for each number, they check if it is prime by iterating from 2 to n and using a prime-checking function. The brute force method has a time complexity of O(n √n), making it inefficient for larger values of n, such as 10^6. The speaker suggests optimizing this approach by reducing the prime-checking time to constant time, which leads into a more efficient algorithm.

05:01

⚙️ Optimizing Prime Checking with Sieve of Eratosthenes

The speaker introduces the Sieve of Eratosthenes, an algorithm that optimizes prime-checking by marking non-prime numbers efficiently. To illustrate, an array is initialized where all values are set to 1 (indicating prime). Starting from 2, the first prime, the algorithm marks all multiples of 2 as 0 (non-prime). This process continues for the next prime, 3, and its multiples. The algorithm ensures that each prime’s multiples are marked as non-prime without checking numbers already marked by smaller primes. This way, a black box is created where checking whether a number is prime becomes a constant-time operation.

10:03

📉 Optimizing the Sieve Further: Avoiding Redundant Marking

The speaker discusses how to further optimize the Sieve of Eratosthenes by starting from i^2 instead of i × 2 when marking multiples of each prime i. The rationale is that smaller multiples of i would have already been marked by smaller primes. For example, for i = 5, there’s no need to mark 5 × 2 = 10, as it would have been marked when processing 2. By starting from i^2, the algorithm avoids unnecessary computations and increases efficiency. Additionally, the speaker introduces the idea of limiting the loop to √n, as numbers beyond that would have their multiples already marked.

15:03

⏱ Final Optimizations and Complexity Analysis

In the final part, the speaker explains the time complexity of the optimized Sieve of Eratosthenes. The marking process runs in O(n log log n), which is more efficient than the brute force approach. The space complexity remains O(n), as it uses an array to store the primality of each number. The speaker reiterates the process, emphasizing that the algorithm performs precomputation to ensure that checking whether a number is prime takes constant time. The video concludes by encouraging viewers to like, subscribe, and check out the code implementations for different programming languages.

Mindmap

Keywords

💡Prime Numbers

A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. In the video, prime numbers are central to the algorithm, as the task involves identifying all prime numbers up to a given value, n. Examples include 2, 3, 5, and 7, which are the primes under 10.

💡Brute Force

Brute force refers to solving a problem by trying all possible solutions, often inefficiently. In the video, the brute force method involves checking every number up to n to determine if it is prime, which has a time complexity of O(n * sqrt(n)). This method becomes too slow for large values of n, such as 10^5 or 10^6.

💡Time Complexity

Time complexity is a way to quantify the amount of time an algorithm takes to run as a function of the input size. In the script, time complexity is frequently mentioned, especially when comparing the brute force approach (O(n * sqrt(n))) and the optimized Sieve of Eratosthenes (O(n log log n)).

💡Space Complexity

Space complexity measures the amount of memory an algorithm uses relative to its input size. The brute force method has a space complexity of O(1) because it uses minimal additional memory, while the optimized algorithm, Sieve of Eratosthenes, has a space complexity of O(n) due to the prime array it creates.

💡Optimization

Optimization refers to improving an algorithm to make it more efficient in terms of time and/or space. In the video, the speaker describes how to optimize the brute force solution by using the Sieve of Eratosthenes, which reduces the time complexity by precomputing prime numbers.

💡Sieve of Eratosthenes

The Sieve of Eratosthenes is an ancient algorithm used to find all primes up to a given number efficiently. It works by iteratively marking the multiples of each prime starting from 2, and it runs in O(n log log n) time. In the video, this algorithm is implemented to optimize prime number generation.

💡Precomputation

Precomputation involves calculating values ahead of time and storing them for quick access during the main algorithm. In this video, the Sieve of Eratosthenes precomputes prime numbers in an array, which allows the program to check if a number is prime in constant time O(1).

💡Multiples

Multiples are numbers that can be divided exactly by another number. In the context of prime number algorithms, multiples of a number (e.g., 2, 3, 5) are marked as non-prime because they have divisors other than 1 and themselves. For example, multiples of 2 such as 4, 6, 8, and 10 are not prime.

💡Array

An array is a data structure that stores elements in a fixed-size sequence. In the Sieve of Eratosthenes algorithm, an array is used to mark whether numbers are prime (1) or non-prime (0). The size of the array is n + 1, where n is the upper limit of numbers to be checked for primality.

💡Mathematical Proof

Mathematical proof is a logical demonstration that a certain proposition or statement is always true. In the video, the speaker mentions that the time complexity of the optimized algorithm is based on a prime harmonic series and is mathematically proven to be O(n log log n), though the proof itself is not shown.

Highlights

Introduction of prime number algorithm with time complexity issues and why brute force is inefficient.

Explanation of the Sieve of Eratosthenes algorithm, focusing on optimizing prime number checks.

Example given with number n=10, demonstrating how to find all prime numbers up to n.

Brute force method discussed with time complexity O(n * sqrt(n)), showing its limitations for larger numbers like 10^6.

Key insight: Multiple of a prime number will never be prime, and marking these multiples can optimize prime detection.

Detailed steps in the Sieve of Eratosthenes: initializing an array with 1s, marking non-prime numbers as 0.

Discussion on starting at 2, the first prime, and marking all multiples of 2 (e.g., 4, 6, 8, etc.) as non-prime.

Moving to the next prime, 3, and repeating the marking process for all multiples of 3.

Prime number identification based on the array values: all remaining 1s represent primes.

Optimization: No need to mark multiples of numbers like 4, 6, 8, since they are already marked as non-prime by smaller primes.

Further optimization: Instead of starting from i * 2, start from i * i to reduce redundant operations.

Explained the importance of stopping the loop once i * i exceeds the number n, as further calculations are unnecessary.

Introduction of the time complexity analysis of Sieve of Eratosthenes, revealing O(n log(log(n))).

Final optimized pseudo code presented, including marking non-prime numbers efficiently.

Conclusion with a discussion on space complexity, which is O(n) due to the array used for prime detection.

Transcripts

play00:03

so let's continue with the math section

play00:05

in sters A to Z DSA course but before

play00:07

that here you welcome back to the

play00:08

channel I hope you guys are doing

play00:09

extremely well so the problem that we

play00:11

will be solving today is C of AAS cross

play00:16

tees something like that so this uh

play00:19

algorithm is something which is related

play00:21

to prime numbers so in order to

play00:23

understand that let's pick up a very

play00:25

simple problem given a number n we have

play00:29

to print all the primes up till n so

play00:32

assume I give you n as 10 so I have to

play00:35

print out all the Primes till the number

play00:37

10 so that will be 2 3 5 and 7 so there

play00:42

are four Prime till then you'll have to

play00:44

print down all the four prime numbers

play00:46

now what is the extreme name solution

play00:49

that you can think of we know how to

play00:51

check a prime number right so it'll be

play00:53

like okay start from I equal to 2

play00:56

because that is the first prime number

play00:58

we can go ahead till n and we can call

play01:02

the prime function that we already know

play01:04

how to write if this is a prime number

play01:07

we can print the prime so this will be

play01:12

the extreme name solution The Brute

play01:14

Force and what will be the D complexity

play01:18

the outer loop is running for B go of N

play01:21

and the prime check we know can be done

play01:22

in square root of n so this will be the

play01:25

time complexity will there be any space

play01:27

complexity no the space complexity will

play01:29

be we go of one obviously this is an

play01:32

extremely expensive operation why

play01:34

imagine I give you n as 10 to the^ 5 and

play01:39

I ask you to print down all the prime

play01:41

numbers till 10 ^ 5 then the time

play01:43

complexity will be B go of 10^ 5 into

play01:47

square root of 10^ 5 which is a lot of

play01:49

time and if I give you 10^ 6 then that's

play01:52

a lot of operations right so that is why

play01:55

this method is something that we will

play01:57

not be following and we'll try to

play01:59

optimize this so what will be the

play02:01

optimized method so what is taking time

play02:04

over here the first one is the loop

play02:08

which is hydrating through each and

play02:10

every integer and then there is a prime

play02:13

check if I can optimize this Prime check

play02:16

and if I can do it in a constant time if

play02:19

I can do the prime check in a constant

play02:22

time then the square root of n will go

play02:24

off and that is what I will be trying to

play02:27

do and that is where this particular

play02:29

algorithm see of era TOS things or

play02:32

something like that comes in so what I

play02:35

will try to do is I'll try to create a

play02:38

black box where if I pass on an integer

play02:42

it tells me if it is a prime or not but

play02:45

this black box will be working in beo of

play02:48

one time complexity so in order to do

play02:51

that what I'll be doing

play02:53

is I'll be taking Nal 30 just to explain

play02:57

you what algorithm I'm thinking of like

play03:00

what is the black box that that I'm

play03:01

thinking of Nal to 30 I'll be declaring

play03:05

a prime array of 31 size that is one

play03:08

more than the number okay and I started

play03:12

the index from two I've omitted the

play03:15

zeroth index and the first index because

play03:17

I know that these numbers are never

play03:19

Prime like one is not a prime number so

play03:21

it'll be starting off at two so to start

play03:23

of what I've done is I've initialized

play03:26

everything with one all the indexes with

play03:28

one till the last index which is 30

play03:31

start from the second index till the

play03:33

30th index everything is one okay now

play03:37

the start of what I'll do is I'll start

play03:39

with the number two and I know that two

play03:42

is a prime number that is for sure

play03:44

because that is the first Prime I'm very

play03:46

sure can I say this if 2 is prime any

play03:50

multiple of two will never be prime

play03:53

something like 2 into 2 which is 4

play03:56

that's never going to be prime because

play03:58

it has a factor two 2 into 3 that is 6

play04:01

will never be prime because it has a

play04:03

factor two for a number to be prime the

play04:06

factors can only be one and the number

play04:09

itself so obviously two is a factor so

play04:11

it cannot be a prime 2 into 4 8 2 into 5

play04:15

10 these numbers cannot be prime I'm

play04:18

very sure of it so what I'll do is I'll

play04:21

be like okay let's go ahead and mark

play04:24

them as zero so I'll go ahead and mark

play04:26

them as zero

play04:28

perfect 0 0 0 0 0 0 0 0 and zero so I've

play04:39

marked every multiple of two as zero

play04:42

till the number that is 30 done so 2 is

play04:47

completed what is the next number three

play04:51

again do the same thing I'll be like

play04:52

okay three is a prime number how do I

play04:55

know it because it is marked as one it

play04:58

is marked as one didn't get a factor

play05:01

before okay fine so what's 3 into 2

play05:04

that's 6 what's 3 into 3 that's 9 so be

play05:08

marking all the multiples of three

play05:10

starting from 6 till 30 as zero let's do

play05:13

it so six is already marked as Z 9 will

play05:17

be marked as 0 12 is already marked 15

play05:19

will be marked as 0 18 is already marked

play05:22

21 will be marked as 0 24 will be marked

play05:25

as 0 27 will be marked as 0 30 is

play05:27

already marked as 0 so 38 is done what

play05:31

is the next number the next number is

play05:34

four tell me one thing is four a prime

play05:38

no it is not because it is already

play05:40

marked as zero which means someone

play05:44

marked it two did Mark it that means it

play05:47

is a multiple of two tell me one thing

play05:50

what are the multiples of

play05:51

four that's eight that's three that's

play05:56

16 all the multiples of four will also

play05:59

be multiples of two agreed basic maths

play06:03

all the multiples of four will be

play06:06

multiples of two as well so do I need to

play06:08

go ahead and Mark 8 12 16 I do not

play06:11

because it is a non- prime number got it

play06:14

so not do anything I'll go to the next

play06:17

number which is five for five is it a

play06:21

prime it is because it is marked as one

play06:24

it didn't find any factor or before it

play06:27

so I'll start off with 5 into 2 that's

play06:30

10 10 is already marked 5 into 3 15 15

play06:34

is already marked 5 into 4 20 20 is

play06:36

already marked ready is already marked 5

play06:40

into 5 25 that's where I mark it zero

play06:44

perfect five is

play06:47

done what's the next number six for six

play06:52

do I need to do do I need to do anything

play06:55

because 6 is not a prime 6 into 2 is

play06:59

basically 12 that would be marked by

play07:02

someone who marked six 6 was marked by 2

play07:06

and three because 2 and three both have

play07:09

multiples as six so they would have

play07:11

marked 12 and 6 into 3 18 as well 6 into

play07:15

4 24 as well so I don't need to do

play07:18

anything got it perfect let's go to the

play07:21

next number seven Fine 7 is 1 7 into 2

play07:29

is

play07:30

14 so you can go ahead and Mark it 7

play07:33

into 3 is 21 you can go ahead and Mark

play07:35

it 7 into 4 is 28 you can go ahead and

play07:37

Mark it all of them were marked so you

play07:41

don't do anything and you move

play07:43

ahead for eight do I need to do no for 9

play07:47

do I need to do no for 10 do I need to

play07:49

do no for 11 do I need to do yes because

play07:52

11 is a prime so 11 into 2 is 22 so you

play07:56

can go ahead and Mark 22 11 into 3 is

play08:00

that is beyond the boundary so you stop

play08:03

perfect for 12 no need for 13 again you

play08:06

can do and you can keep on doing you can

play08:08

keep on doing at the end of the day this

play08:12

is the array that you will be having at

play08:14

the end of the day this is the array

play08:16

that you will be having

play08:18

right if I see what numbers are marked

play08:21

as one so two is marked as one three is

play08:26

marked as one five is marked as one 7 is

play08:29

marked as 1 11 is 13 is 17 is 19ers and

play08:35

29ers and ready three how is 23 is 23 a

play08:40

prime yeah it is a prime 23 is also

play08:43

marked as one so all of these numbers

play08:46

that are priming till 30 are marked as

play08:49

one apart from that all the numbers are

play08:52

marked as zero very simple now I have an

play08:55

AR I have an AR primes if I ask you hey

play09:01

if five is a prime so you'll be like

play09:03

Prime of five equal to equal to 1 yes it

play09:07

is a prime so I have a black box that

play09:10

can tell me the answer and be go fun but

play09:13

what I need to do is some

play09:15

precomputation some precomputation is

play09:19

what I need to do now this

play09:21

pre-computation is known as save of

play09:25

aosth got

play09:27

it I I'll write the code then we'll try

play09:29

optimizing that as well the code is very

play09:32

simple function which takes n I'll have

play09:36

to print down all the prime numbers till

play09:38

in so first of all we can have a primary

play09:42

of size n + 1 please make sure you run a

play09:46

fall loop from 2 to n and Mark

play09:51

everything as one Mark everything as one

play09:54

so that's very simple you can start off

play09:56

the loop from Two and you can go on till

play09:58

n and you can say Prime of I equal to 1

play10:03

if you're using C++ you can use memet

play10:06

for Java there might be something like

play10:07

that you can use it as well so this is

play10:10

what I'll do initially done what is my

play10:13

next job my next job is to iate from the

play10:16

number

play10:17

two till

play10:19

n and what do I do I'm saying hey if the

play10:23

number is prime if it is marked as one

play10:27

if it is marked as one

play10:30

then I go over it's multiple and Mark

play10:32

all the multiples as zero that's what

play10:34

I'm doing okay so I'm saying okay fine

play10:38

I'll start a Lo J and that starts from 2

play10:41

into I and J goes till n because that is

play10:45

the max I need and will it be

play10:48

j++ no it won't be j++ it will be j+

play10:52

equal to i

play10:54

y if if I take

play10:56

five 5 into 2 is 10

play11:00

5 into 3 is 15 so a shift of five

play11:03

there's a shift of five that is why I'm

play11:05

doing plus I that is why I'm doing plus

play11:07

I got it okay fine what do what do I do

play11:12

I Mark all the multiples as Z that is

play11:16

the thing that I'm doing if is completed

play11:19

for Loop is completed I'll not be

play11:21

calculating the time complexity because

play11:24

you cannot pinpoint the time complexity

play11:26

of this one this Loop is easy easy to

play11:29

compute but this one will be varing

play11:31

according to the ey so we cannot

play11:33

pinpoint I try to optimize this is this

play11:37

the most optimal solution probably not

play11:41

but you have your Prime ready you have

play11:44

your Prime array ready so now in order

play11:47

to print all the prime numbers till n

play11:49

what you need to do is need to run Loop

play11:51

till n and you need to say

play11:53

if Prime of I is equal toal to 1 you

play11:58

just have to print I so I've trimmed

play12:01

down trimmed down the square root of n

play12:04

into B go of one the only thing I need

play12:07

to do is make sure this pre-computation

play12:09

is done in the most optimal time

play12:12

possible so so now I'll try to optimize

play12:14

this let's try to optimize

play12:17

this some

play12:20

observations so the first observation

play12:23

some so in order to optimize this we'll

play12:26

have to do some observations let's start

play12:28

with the first observation it's very

play12:31

simple we started with the first Prime

play12:34

two and we marked 2 into two then 2 into

play12:37

3 then 2 into 4 then 2 into 5 and so on

play12:42

for the next prime which is three we

play12:45

started marking 3 into 2 3 into 3 3 into

play12:48

4 3 into 5 and so on for the next prime

play12:52

five we started 5 into 2 5 into 3 5 into

play12:56

4 5 into 5 and so on for the next PL 7

play13:00

into 2 7 into 3 7 into 4 7 into 5 7 into

play13:05

6 and 7 into 7 okay tell you one

play13:11

thing for this two it is absolutely okay

play13:15

to start with 2 into two but for this

play13:18

three I'm saying 3 into two I'm saying 3

play13:21

into two do I need to do it because this

play13:23

is already been marked over here the St

play13:27

have already marked three into 2 right

play13:30

so do I need to start from 3 into 2

play13:32

doesn't make sense 3 into 3 is the first

play13:35

number that I start from 9 and then I go

play13:38

ahead for 12 12 would be marked but that

play13:41

is okay but if I start from 3 into three

play13:44

will it work it will let's look it over

play13:46

here 5 into 2 would already been marked

play13:49

by 5 into 2 2 into 5 2 into 5 2 into 5

play13:53

would have already marked 3 into 5 would

play13:56

have already been marked here 5 into 4

play13:59

which is 20 and four is a multiple of

play14:02

two so that have been already been M by

play14:05

two okay so you actually start from 5

play14:08

into 5 which is 25 and that's the first

play14:11

number youa same with 7 7 into 2 would

play14:15

have been marked by two 3 into

play14:17

7 by 2 by 5 and by two or three 42 cuz

play14:23

this is 42 which would be marked by

play14:25

either two or three agreed so you

play14:29

actually start from 7 into

play14:32

7 so what I observe is I don't need to

play14:35

start from 7 into two like basically I

play14:39

don't need to start from I into two I

play14:42

can straight away start from I into I I

play14:46

can straight away start from I into I

play14:51

okay perfect I can straight away start

play14:53

from I into I I know this one

play14:56

optimization done what is the next

play14:58

optimization

play15:00

if I'm starting from I into y let's take

play15:03

the number

play15:04

as the number is 30 right n is

play15:09

30 and the moment I reach 6 the moment I

play15:15

reach six what happens is I is six okay

play15:22

and this is

play15:23

false fine after that I is 7 and this is

play15:28

2 true what happens to the J Loop is 7

play15:33

into 7 that's 49 49 lesser than 30 is

play15:39

false so actually you don't Mark

play15:42

anything beond you don't mark because 7

play15:44

into 7 have exceeded your number right

play15:48

so do I need to go till end I don't need

play15:53

because you have a condition as I into y

play15:56

so the max you need to go is till square

play16:00

root of 30 that's 5 something see if you

play16:03

go till five it will work if you go till

play16:06

5 it will look because for five the

play16:09

inner loop will be 5 into 5 that's 25

play16:12

and you'll Mark 25 as

play16:15

zero got it so you don't need to Loop

play16:18

till this instead of it you can do

play16:21

square root of n so you can write the

play16:23

for loop as for I = 2 I into I lesser

play16:29

than n and I ++ if you write this it

play16:34

will be absolutely fine so you're

play16:36

optimizing and we have optimized a lot

play16:40

so you might be thinking okay we're

play16:42

doing a lot of pre-computation but what

play16:44

is the time complexity first of all

play16:46

we're running a bigo of n Loop in order

play16:49

to mark all the numbers as one so that's

play16:52

a bigo of in this one I cannot explain

play16:56

you because this is something which is

play16:58

math ma atically proven and the time

play17:00

complexity is n logarithmic of log of n

play17:05

that is the time complexity and it's

play17:07

because it's a prime harmonic

play17:11

series and no one will be asking you to

play17:13

derive the time complexity because it is

play17:17

extremely complicated so do not get into

play17:20

it just remember that the time

play17:21

complexity is n log of login it's not

play17:25

login it's log of login okay and after

play17:28

that this is another Meo of n so in

play17:32

order to print all the Primes till n the

play17:35

time complexity is B of n plus n

play17:40

logarithmic log of n plus b of n again

play17:45

this is a pseudo code you find the C++

play17:48

Java Python and JavaScript code given

play17:50

below you can check it out and what will

play17:53

be the space complexity I'm using an

play17:55

extra space of B go and in order to make

play17:58

make sure I create that black box got it

play18:03

so that was it for SE of aats te I hope

play18:05

that I got that correct so if you

play18:07

understood everything please please do

play18:09

consider giving us a like and if you

play18:11

need to our channel do consider

play18:12

subscribing to us as well with this I'll

play18:14

be wrapping up this video Let's Wait in

play18:15

some other video till then byebye take

play18:19

care

play18:22

broken

play18:25

golden

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
Prime NumbersDSAAlgorithmsSieve MethodMath OptimizationCode TutorialTime ComplexityProgrammingCoding TipsPrime Check
¿Necesitas un resumen en inglés?