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

2 Comments

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

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

@na3744
na3744 Feb 23, 2024 at 2:52 AM

I bought this course, it worth it!

@mk.info.work
mk.info.work Nov 15, 2023 at 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

@sciaku1
sciaku1 Jan 11, 2024 at 3: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 About Sciaku Courses & Services

Quick answers to common questions about our courses, quizzes, and learning platform

Didn't find what you're looking for?

help_center Contact Support