Team Emertxe
Basic Refresher
Assignment 12
Assignment 12
Assignment 12
WAP to print all prime numbers <= ‘num’ using Sieve of
Eratosthenes method.
Assignment 12
WAP to print all prime numbers <= ‘num’ using Sieve of
Eratosthenes method.
Input:
Assignment 12
WAP to print all prime numbers <= ‘num’ using Sieve of
Eratosthenes method.
Input: Read integer ‘num’
Assignment 12
WAP to print all prime numbers <= ‘num’ using Sieve of
Eratosthenes method.
Input: Read integer ‘num’
Output:
Assignment 12
WAP to print all prime numbers <= ‘num’ using Sieve of
Eratosthenes method.
Input: Read integer ‘num’
Output: Print the prime numbers less than or equal to ‘num’
Assignment 12
What is a Prime number?
Assignment 12
What is a Prime number?
 A number that is divisible only by itself and 1.
Assignment 12
What is a Prime number?
 A number that is divisible only by itself and 1.
 Examples: 2, 3, 5, 7, 11 etc.
Assignment 12
Sieve of Eratosthenes:-
Assignment 12
Sieve of Eratosthenes:-
⮚The sieve of Eratosthenes is one of the most efficient ways to
find all of the smaller primes.
Assignment 12
Sieve of Eratosthenes:-
⮚The sieve of Eratosthenes is one of the most efficient ways to
find all of the smaller primes.
⮚Generate a list of integers from 2 to 20
Assignment 12
Sieve of Eratosthenes:-
⮚The sieve of Eratosthenes is one of the most efficient ways to
find all of the smaller primes.
⮚Generate a list of integers from 2 to 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Assignment 12
Sieve of Eratosthenes:-
⮚The sieve of Eratosthenes is one of the most efficient ways to
find all of the smaller primes.
⮚Generate a list of integers from 2 to 20
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Assignment 12
Sieve of Eratosthenes:-
⮚The first number in the list is 2; cross out every multiples of
2.
Assignment 12
Sieve of Eratosthenes:-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
X
i
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Assignment 12
Sieve of Eratosthenes:-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
X X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
i
Assignment 12
Sieve of Eratosthenes:-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
X X X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
i
Assignment 12
Sieve of Eratosthenes:-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
X X X
X X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
i
Assignment 12
Sieve of Eratosthenes:-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
X X X
X X X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
i
Assignment 12
Sieve of Eratosthenes:-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
X X X
X X X X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
i
Assignment 12
Sieve of Eratosthenes:-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
X X X
X X X X X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
i
Assignment 12
Sieve of Eratosthenes:-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
X X X
X X X X X X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
i
Assignment 12
Sieve of Eratosthenes:-
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
X X X
X X X X X X X
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
i
Assignment 12
Sieve of Eratosthenes:-
⮚ Can we remove array elements?
Assignment 12
Sieve of Eratosthenes:-
⮚ Can we remove array elements?
We cannot remove array elements
Assignment 12
Sieve of Eratosthenes:-
⮚ Can we remove array elements?
We cannot remove array elements
⮚What can be done?
Assignment 12
Sieve of Eratosthenes:-
⮚ Can we remove array elements?
We cannot remove array elements
⮚What can be done?
Make it as 0
Assignment 12
Sieve of Eratosthenes:-
⮚ Can we remove array elements?
We cannot remove array elements
⮚What can be done?
Make it as 0
2 3 0 5 0 7 0 9 0 11 0 13 0 15 0 17 0 19 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Assignment 12
Sieve of Eratosthenes:-
⮚The next number in the list is 3, make multiples of 3 as 0
2 3 0 5 0 7 0 9 0 11 0 13 0 15 0 17 0 19 0
i
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Assignment 12
Sieve of Eratosthenes:-
⮚ The next number in the list is 3, make multiples of 3 as 0
2 3 0 5 0 7 0 0 0 11 0 13 0 15 0 17 0 19 0
i
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Assignment 12
Sieve of Eratosthenes:-
⮚ The next number in the list is 3, make multiples of 3 as 0
2 3 0 5 0 7 0 0 0 11 0 13 0 0 0 17 0 19 0
i
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Assignment 12
Sieve of Eratosthenes:-
⮚The next number which is not removed is 5.
2 3 0 5 0 7 0 0 0 11 0 13 0 0 0 17 0 19 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
i
Assignment 12
Sieve of Eratosthenes:-
⮚ The next number which is not removed is 5.
⮚It is not necessary to remove multiples of 5, because all non
prime numbers has been removed.
2 3 0 5 0 7 0 0 0 11 0 13 0 0 0 17 0 19 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
i
⮚Print all the non-zero numbers from the list.
Assignment 12
Sieve of Eratosthenes:-
2 3 0 5 0 7 0 0 0 11 0 13 0 0 0 17 0 19 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
⮚Prime numbers are:
2, 3, 5, 7, 11, 13, 17, 19
Assignment 12
Sieve of Eratosthenes:-
Assignment 12
Example’s:
Assignment 12
Example’s:
⮚Input:
Assignment 12
Example’s:
⮚Input: num = 20
Assignment 12
Example’s:
⮚Input: num = 20
Output:
Assignment 12
Example’s:
⮚Input: num = 20
Output: 2, 3, 5, 7, 11, 13, 17, 19
Assignment 12
Example’s:
⮚Input: num = 20
Output: 2, 3, 5, 7, 11, 13, 17, 19
⮚Input:
Assignment 12
Example’s:
⮚Input: num = 20
Output: 2, 3, 5, 7, 11, 13, 17, 19
⮚Input: num = 30
Assignment 12
Example’s:
⮚Input: num = 20
Output: 2, 3, 5, 7, 11, 13, 17, 19
⮚Input: num = 30
Output:
Assignment 12
Example’s:
⮚Input: num = 20
Output: 2, 3, 5, 7, 11, 13, 17, 19
⮚Input: num = 30
Output: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
Assignment 12
Example’s:
⮚Input: num = 20
Output: 2, 3, 5, 7, 11, 13, 17, 19
⮚Input: num = 30
Output: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
⮚Input:
Assignment 12
Example’s:
⮚Input: num = 20
Output: 2, 3, 5, 7, 11, 13, 17, 19
⮚Input: num = 30
Output: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
⮚Input: num = -20
Assignment 12
Example’s:
⮚Input: num = 20
Output: 2, 3, 5, 7, 11, 13, 17, 19
⮚Input: num = 30
Output: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
⮚Input: num = -20
Output:
Assignment 12
Example’s:
⮚Input: num = 20
Output: 2, 3, 5, 7, 11, 13, 17, 19
⮚Input: num = 30
Output: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29
⮚Input: num = -20
Output: Please enter a positive number which is > 1
Sample execution:-
Assignment 12
Sample execution:-
Assignment 12
Sample execution:-
Assignment 12
Assignment 12
Pre-requisites:-
Assignment 12
Pre-requisites:-
⮚Loops
Assignment 12
Pre-requisites:-
⮚Loops
⮚Operators
Assignment 12
Pre-requisites:-
⮚Loops
⮚Operators
⮚Arrays
Assignment 12
Pre-requisites:-
⮚Loops
⮚Operators
⮚Arrays
Objective:-
Assignment 12
Pre-requisites:-
⮚Loops
⮚Operators
⮚Arrays
Objective:-
⮚To understand the concept of if-else constructs and arrays.
Team Emertxe
Thank you

12_prime.pdf