Prime Numbers From List Function

조회 수: 2 (최근 30일)
Catalytic Kys
Catalytic Kys 2019년 12월 2일
댓글: Walter Roberson 2019년 12월 2일
I have to make a function that gives all the prime numbers from a list [2:N]
The following steps is what I came up with:
1. First we make a list of number from 2 to 10. We start with a tester equal to 2, and remove all multiples of 2 from the list.
2. We look at the next remaining value, which is 3. Our tester now becomes equal to
3. We again remove al multiples of 3 from the list. 3. Again, we look at the next remaining value. tester is now 5, we remove al multiples of 5 (of which there are none).
4. We do this one last time with 7, of which there are also no multiples.
5. The final list contains all primes up to 10.
10 should be variable N and the range of numbers should be equal to 2:N
Could someone help me how I put this in function form?
So that the function getPrimeNumbers(N) returns all the prime values from the list [2:N]
All help is greatly appreciated!
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 12월 2일
Search for Eratosthenes in MATLAB Answers. You will find numerous implementations.

댓글을 달려면 로그인하십시오.

답변 (1개)

Catalytic Kys
Catalytic Kys 2019년 12월 2일
Still looking for an answer
  댓글 수: 4
Steven Lord
Steven Lord 2019년 12월 2일
The primes function is implemented as a MATLAB function file (which is what I suspect you mean by "in code form".)
Walter Roberson
Walter Roberson 2019년 12월 2일
That's odd. I just looked through https://www.mathworks.com/matlabcentral/answers/?term=tag%3A%22eratosthenes%22 and found full source code (and also a vectorized version.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by