Optimising Sieve & Segmented Sieve

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 88:- Optimising Sieve & Segmented Sieve

Sieve of Eratosthenes is an efficient algorithm to find all prime numbers up to a given limit 'n'. Segmented Sieve is an optimization of the original Sieve, which allows us to find primes in a specific range [l, r].

  1. Optimizing Sieve of Eratosthenes: The basic Sieve of Eratosthenes algorithm goes as follows:

pythonCopy code

def sieve_of_eratosthenes(n):    prime = [True] * (n + 1)    p = 2    while p * p <= n:        if prime[p] == True:            for i in range(p * p, n + 1, p):                prime[i] = False        p += 1        primes = [p for p in range(2, n + 1) if prime[p]]    return primes

This implementation finds all prime numbers up to 'n' in O(n log log n) time complexity.

  1. Optimizing Segmented Sieve: Segmented Sieve allows us to find prime numbers in a specific range [l, r]. It uses the concept of the Sieve of Eratosthenes but applies it to smaller segments of the range.

pythonCopy code

import math def simple_sieve(limit, primes):    prime = [True] * (limit + 1)    p = 2    while p * p <= limit:        if prime[p] == True:            for i in range(p * p, limit + 1, p):                prime[i] = False        p += 1    for p in range(2, limit + 1):        if prime[p]:            primes.append(p) def segmented_sieve(l, r):    limit = int(math.sqrt(r)) + 1    primes = []    simple_sieve(limit, primes)    n = r - l + 1    mark = [True] * n    for i in range(len(primes)):        loLim = int(math.floor(l / primes[i])) * primes[i]        if loLim < l:            loLim += primes[i]        for j in range(loLim, r + 1, primes[i]):            mark[j - l] = False    result = [l + i for i in range(n) if mark[i]]    return result

This implementation finds all prime numbers in the range [l, r] in O((r - l + 1) * log log r) time complexity.

Both the optimized Sieve of Eratosthenes and the Segmented Sieve are important tools when working with prime numbers and are frequently used in competitive programming and other algorithmic tasks.

12. Basics Maths and Pointers

Comments: 2

profile
@mk.info.work
17-Feb-2024, 10:20 PM

SCIAKU Team please upload 1st video of TREE please please please, please

profile
@na3744
23-Feb-2024, 02:52 AM

I bought this course, it worth it!

profile
@mk.info.work
15-Nov-2023, 10:25 PM

Hi i want to buy this course but you dont have master card payment method please let me know how i can buy it

profile
@sciaku1
11-Jan-2024, 03:23 PM

Dear mk.info.work, Now we have all types of payment options. If you need to purchase just checkout our official website

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?