Matlab code for Eratosthenes
조회 수: 18 (최근 30일)
이전 댓글 표시
I wrote a code for the sieve of Eratosthenes, but the grader was not satisfied with my code. I'm not sure what I did wrong to his eyes. Any ideas? Please and thank you!
function p = prime_seive(n)
p = [0 2:n];
for k = 2:sqrt(n)
if p(k)
p(k^2:k:n) = 0;
end
end
p = p(p ~= 0);
end
댓글 수: 5
채택된 답변
Jan
2021년 4월 29일
The code works fine and replies the correct values for:
[], 1, 2, 3, 10, 100, 200
Maybe inputs like NaN or vectors should be caught also? Is a column vector expected as output?
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Manage Products에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!