Help with MatLab, from beginning to end?

3 次查看(过去 30 天)
The sieve of Eratosthenes is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite (i.e., not prime) the multiples of each prime, starting with the multiples of 2.1
To find all the prime numbers less than or equal to 20, proceed as follows. First generate a list of integers from 2 to 20: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
The first number in the list is 2; cross out every 2nd number in the list after it by counting up from 2 in increments of 2 (these will be all the multiples of 2 in the list): 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
The next unmarked number in the list after 2 is 3; cross out every 3rd number in the list after it by counting up from 3 in increments of 3 (these will be all the multiples of 3 in the list): 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
The next unmarked number in the list after 3 is 5; the next step would be to cross out every 5th number in the list after 5, but they are all already crossed out at this point, as these numbers (10, 15, 20) are also multiples of smaller primes because 5*5 is greater than 25. The numbers left not crossed out in the list at this point are all the prime numbers below 20: 2 3 5 7 11 13 17 19
Write a script that prompts the user for a number and then finds all of the primes up to that number by using the sieve of Eratosthenes.

回答(1 个)

Walter Roberson
Walter Roberson 2016-3-30
That kind of tutoring "from start to finish" takes hours and hours, and requires a lot of back and forth interaction, as we have no idea what your familiarity level is with programming or MATLAB.
Speaking for myself: I would not even attempt that kind of tutoring except in-person -- and if not outright paid then at the very least for a non-trivial charitable donation tax deduction. It is hard work for the teacher.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by