Implement the sieve of Eratosthenes: a function for computing prime numbers, known to the ancient Greeks. Choose an integer n. This function will compute all prime numbers up to, and including, n. First insert all numbers from 1 to n into a set. Then erase all multiples of 2 (except 2); that is, 4, 6, 8, 10, 12, ... . Erase all multiples of 3, that is, 6, 9, 12, 15, ... . Go up to ?⎯⎯√n. The remaining numbers are all primes. Make sure that you use functions in your solution, including a main function. Define def sieveOfEratosthenes(n) that receives the maximum value and returns a list of all prime numbers less than or equal to this value in main() print the list of prime numbers.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 9SA
icon
Related questions
Question
  • Implement the sieve of Eratosthenes: a function for computing prime numbers, known to the ancient Greeks. Choose an integer n. This function will compute all prime numbers up to, and including, n.
    • First insert all numbers from 1 to n into a set.
    • Then erase all multiples of 2 (except 2); that is, 4, 6, 8, 10, 12, ... .
    • Erase all multiples of 3, that is, 6, 9, 12, 15, ... .
    • Go up to ?⎯⎯√n.
  • The remaining numbers are all primes.
  • Make sure that you use functions in your solution, including a main function.
  • Define def sieveOfEratosthenes(n) that receives the maximum value and returns a list of all prime numbers less than or equal to this value
  • in main() print the list of prime numbers.
Enter a positive integer: 29
prime numbers up to 29 are:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Transcribed Image Text:Enter a positive integer: 29 prime numbers up to 29 are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning