Matlab code for Eratosthenes

31 ビュー (過去 30 日間)
Aaron Millan
Aaron Millan 2021 年 4 月 29 日
コメント済み: Krystian 2024 年 10 月 21 日
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 件のコメント
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 日
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 件)

カテゴリ

Help Center および File ExchangeManage Products についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by