0% found this document useful (0 votes)
102 views3 pages

Sieve of Eratosthenes:: Topics That You Should Know With Sieve

The document discusses algorithms for solving number theory problems related to prime numbers, including modular exponentiation, greatest common divisor (GCD), the extended Euclidean algorithm, and modular multiplicative inverse. It describes the sieve of Eratosthenes algorithm for finding prime numbers in a range, and its optimized segmented sieve variation. It provides tutorials and practice problems related to these topics, ranging from easy to hard difficulties. The key algorithms covered are the sieve of Eratosthenes for finding primes up to 10^6, and the segmented sieve for efficiently finding primes between larger ranges like 10^6 to 10^12.

Uploaded by

Keyur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views3 pages

Sieve of Eratosthenes:: Topics That You Should Know With Sieve

The document discusses algorithms for solving number theory problems related to prime numbers, including modular exponentiation, greatest common divisor (GCD), the extended Euclidean algorithm, and modular multiplicative inverse. It describes the sieve of Eratosthenes algorithm for finding prime numbers in a range, and its optimized segmented sieve variation. It provides tutorials and practice problems related to these topics, ranging from easy to hard difficulties. The key algorithms covered are the sieve of Eratosthenes for finding primes up to 10^6, and the segmented sieve for efficiently finding primes between larger ranges like 10^6 to 10^12.

Uploaded by

Keyur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Topics that you should know with sieve​:

1. Modular exponentiation
2. Greatest Common Divisor (GCD)
3. Extended Euclidean algorithm
4. Modular multiplicative inverse

Tutorial:
https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/tutorial/

Problems:

1. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/practi
ce-problems/algorithm/calculate-the-power/
2. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/practi
ce-problems/algorithm/name-count/
3. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/practi
ce-problems/algorithm/beautiful-primes/description/
4. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/practi
ce-problems/algorithm/diedie/

Once you are done with all these topics,you can move on to the next topic.

Sieve of eratosthenes:

While solving problems related to number theory we all come across situation where we
need to find prime numbers from ​L to R,​number of divisors of given number,factors of given
number etc.All of these problem could be solved using one efficient algorithm called seive.It has
many variants.

Tutorial:

https://www.geeksforgeeks.org/sieve-of-eratosthenes/
https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-2/tutorial/

Please go through both tutorials carefully before moving on to the problems.

Once you are done with tutorials,you should know how to these things efficiently.
1. Prime numbers in given range ​L to R. ​ (1<=L<=R<= 106 )
2. Count number of divisors of given number
3. Prime factorization of given number in O(logn)
Problem: ​Find all prime numbers between ​L to R. ​ ( 1<=L<=R<= 1012 ) ( R-L<= 106 )

How would you solve this???


As you have seen,Simple Sieve has limitation that it requires space of O(n). So for bigger
numbers,we cannot always create array on size n. Here comes the new approach to find
number of primes between ​L to R ​called segmented sieve.

We will find all numbers between 1 to 106 . We will store them in array say ​primes.​Now we will
iterate through all members of ​primes ​and multiples of ​p[i] between given range(L to R) ​will
be marked as non-prime.Unmarked numbers between ​L to R ​will be our prime numbers.

NOTE: ​Why considering primes between 1 to 106 is enough ?


(Think yourself.Answer is given at the end of document.)

Tutorial for Segmented Seive:


https://www.geeksforgeeks.org/segmented-sieve/

Problems on sieve:

Since these problems are very time sensitive… Use FAST i/o to avoid TLE.

Easy

1. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/practi
ce-problems/algorithm/can-you-guess/

2. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/practi
ce-problems/algorithm/hell-1/

3. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/practi
ce-problems/algorithm/archery-1/

4. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-2/practi
ce-problems/algorithm/sum-of-primes-7/

5. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-2/practi
ce-problems/algorithm/ashu-and-prime-factors-4​/

Medium:
1. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/practi
ce-problems/algorithm/gotta-beat-em-all/

2. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-2/practi
ce-problems/algorithm/nearest-prime-a828361b/

3. https://www.hackerearth.com/ja/practice/math/number-theory/basic-number-theory-2/pra
ctice-problems/algorithm/holi-and-division-function-1dfc3294/

Hard:

1. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-1/practi
ce-problems/algorithm/kala-set/

2. https://www.hackerearth.com/practice/math/number-theory/basic-number-theory-2/practi
ce-problems/algorithm/mike-and-gcd-issues/

Answer:

1.​ Suppose prime number x is between 10^6 to 10^12. Then multiples of this prime number till 10^12 will
contain at least one prime which is in range 1 to 10^6.hence primes till 10^6 will be enough.

This can be also proved using fact that if two prime numbers greater than 10^6 say x1,x2 construct a
number y,then y will definitely exceed range of 10^12.

If you want to learn about prime factorization or want to do some easy math questions check
this doc ,

https://docs.google.com/document/d/1fI2xLi8afbcv4w525DeCZW4Y0hw5gK8AIRdYK0itdf4/edit
?usp=sharing

You might also like