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

Stephen23
Stephen23 2021년 4월 29일
편집: Stephen23 2021년 4월 29일
Which refinement of the sieve are you supposed to implement?
Aaron Millan
Aaron Millan 2021년 4월 29일
I was given a pseudocode to work with, and in that code was a "for loop" where
k = 2:sqrt(n), and if pk was not zero, then we set p(j) equal to zero where j = k^2, k^2 +k, k^2 +2k...n
Stephen23
Stephen23 2021년 4월 29일
편집: Stephen23 2021년 4월 29일
Did the grader give any hints or feedback? Which cases failed?
Have you checked the requirements for the vector orientation?
Aaron Millan
Aaron Millan 2021년 4월 29일
Thankfully, they didn't ask for any specific vector orientation. I can't post a picture of the instructions here due to copyright, but let me wite out the two theorems they wanted included:
1) if c is not divisible by any number less than c, then c is prime
2) If c is not divisible by any interger between 2 and sqrt(n), then c is prime
Maybe he was just being a stickler for the pervious parts.
Krystian
Krystian 2024년 10월 21일
maybe the wrong spelling of "sieve" in the function's name?

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

 채택된 답변

Jan
Jan 2021년 4월 29일

0 개 추천

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?

댓글 수: 1

Aaron Millan
Aaron Millan 2021년 4월 29일
I actually just needed an array. this grader has given me issues before, though. Thank you for checking my code!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Manage Products에 대해 자세히 알아보기

질문:

2021년 4월 29일

댓글:

2024년 10월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by