Cody - "server not available" when submitting solution on prime numbers question
조회 수: 6 (최근 30일)
이전 댓글 표시
I am new to matlab and have been going through some of the cody problems, I finished some of the easier ones without a problem and decided to try out the group Prime Numbers III.
When I submit my solution, I get the error message "The server is not available. Wait a few minutes, and then retry your request. If the problem persists, contact the instructor."
I'm guessing this means that my solution is not fast enough?
- Am I right in thinking that this is due to my solution not running fast enough?
- How can I improve the speed of my solution?
Below is my solution to the problem list the moran numbers.
function y = Moran(n)
i = 1:n
for idx = 1:n
a = num2str(idx);
b(idx) = idx/sum(a-'0');
end
b
y = i(ismember(b,primes(n))==1)
end
I also got copilot to generate the below version where there is no for-loop but I dont really understand what it's doing, and seems like it's not fast enough either.
function y = Moran(n)
i = 1:n;
a = arrayfun(@num2str, i, 'UniformOutput', false);
b = cellfun(@(x) str2double(x) / sum(x - '0'), a);
y = i(ismember(b, primes(n)));
end
댓글 수: 0
채택된 답변
Cris LaPierre
2025년 6월 3일
편집: Cris LaPierre
2025년 6월 4일
Currently, I can generate the same error if I submit an infinite while loop. This does suggest the error might be related to long execution times, though it is returning the error quicker than I'd expect.
In addition, it is not an issue with the code you have written. When I test the problem in desktop MATLAB, your solution passes all the tests. However, I found that Test 3 takes an exceptionally long time to run (almost 2 mins).
댓글 수: 5
Cris LaPierre
2025년 6월 4일
편집: Cris LaPierre
2025년 6월 4일
You can get a rough estimate by observing the progress (indicated by the line numbers in a live script).
For a more advanced breakdown, this page talks about how to profile your code: https://www.mathworks.com/help/matlab/matlab_prog/profiling-for-improving-performance.html
Cris LaPierre
2025년 6월 4일
I did notice other solutions used num2str successfully. They included the second optional argument to specify the format. Something like this:
num2str(A','%08d')'-'0');
However, this alone isn't enough to fix your original code.
추가 답변 (1개)
the cyclist
2025년 6월 3일
That error does not mean your solution was not fast enough. (As I recall, there is a separate message telling you if you solution timed out.)
MathWorks has been undergoing a ransomware attack. You can see status updates. Cody is currently listed as operating normally, but it is possible that you got caught in that.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
