Individual Exercise: Sieve of Eratosthenes O The Sieve of Eratosthenes is a technique for finding all the prime numbers up to a given last number. o It can be implemented as a program by using the algorithm (steps) given below. " Create a variable for the last number initialized as 100 etc. (e.g., int lastNumber = 100;) • Create a boolean array isPrime[] to keep track of which numbers up to lastNumber are prime. O The length of the array must be equal to lastNumber + 1 as the array indexes start from zero. O Note: The default value is false for all the array elements when a boolean array is created by using new. • Set all the array elements beginning from index = 2 (the first prime number) to true. ' Outer loop: Repeat the following steps for each number num within 2 <= num <= lastNumber / num. o if isPrime[num] is true (meaning that num is prime as no factors exist for num): * Inner loop: Set the isPrime[] elements for all the multiples of num to false. O When the nested loop ends, isPrime[num] is true if and only if num is a prime number. O Print all the prime numbers up to lastNumber as the numbers (num) for which the corresponding element in the isPrime array (i.e., isPrime[num]) is true. Expected Console Output of the Program for lastNumber = 100: All the prime numbers up to 100 are 2357 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Individual Exercise: Sieve of Eratosthenes
O The Sieve of Eratosthenes is a technique for finding all the prime numbers up to a given last number.
o It can be implemented as a program by using the algorithm (steps) given below.
" Create a variable for the last number initialized as 100 etc. (e.g., int lastNumber = 100;)
• Create a boolean array isPrime[] to keep track of which numbers up to lastNumber are prime.
O The length of the array must be equal to lastNumber + 1 as the array indexes start from zero.
o Note: The default value is false for all the array elements when a boolean array is created by using new.
' Set all the array elements beginning from index = 2 (the first prime number) to true.
' Outer loop: Repeat the following steps for each number num within 2 <= num <= lastNumber / num.
o if isPrime[num] is true (meaning that num is prime as no factors exist for num):
" Inner loop: Set the isPrime[] elements for all the multiples of num to false.
O When the nested loop ends, isPrime[num] is true if and only if num is a prime number.
O Print all the prime numbers up to lastNumber as the numbers (num) for which the corresponding element in
the isPrime array (i.e., isPrime[num]) is true.
Expected Console Output of the Program for lastNumber = 100:
All the prime numbers up to 100 are
2357 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Transcribed Image Text:Individual Exercise: Sieve of Eratosthenes O The Sieve of Eratosthenes is a technique for finding all the prime numbers up to a given last number. o It can be implemented as a program by using the algorithm (steps) given below. " Create a variable for the last number initialized as 100 etc. (e.g., int lastNumber = 100;) • Create a boolean array isPrime[] to keep track of which numbers up to lastNumber are prime. O The length of the array must be equal to lastNumber + 1 as the array indexes start from zero. o Note: The default value is false for all the array elements when a boolean array is created by using new. ' Set all the array elements beginning from index = 2 (the first prime number) to true. ' Outer loop: Repeat the following steps for each number num within 2 <= num <= lastNumber / num. o if isPrime[num] is true (meaning that num is prime as no factors exist for num): " Inner loop: Set the isPrime[] elements for all the multiples of num to false. O When the nested loop ends, isPrime[num] is true if and only if num is a prime number. O Print all the prime numbers up to lastNumber as the numbers (num) for which the corresponding element in the isPrime array (i.e., isPrime[num]) is true. Expected Console Output of the Program for lastNumber = 100: All the prime numbers up to 100 are 2357 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY