Cody - "server not available" when submitting solution on prime numbers question

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?
  1. Am I right in thinking that this is due to my solution not running fast enough?
  2. 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

 Accepted Answer

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 Comments

Sorry, I should have mentioned that the code seems to run fairly quickly without an issue when I tested it on desktop MATLAB.
Thank you for testing and clarifying.
I noticed that some people had managed to complete this problem, which means there must be a solution that works. After profiling the code, I noticed that most of the execution time of your code was spent calculating the sum of the digits. Specifically, converting to char.
That got me wondering if there was a way to do this without employing this technique. I discovered there is an equation for this: https://en.wikipedia.org/wiki/Digit_sum
So you can create a for loop that cycles for the number of digits in n and sum the numbers along the way. This is much faster, and will allow you to pass this problem. You do still want to vectorize as much of this as you can for speed. Updating the code you came up with to use this solution might look like this.
function y = Moran(n)
A = (1:n);
k = floor(log10(n));
B=zeros(size(A));
for i = 0:k
B = B + (mod(A,10^(i+1)) - mod(A,10^i))./10^i;
end
Q = A./B;
y = A(ismember(Q,primes(n))==1);
end
Apologies for all the confusion I'm causing you.
Thank you very much for your detailed answer.
I didnt realise that the conversion to string was so time consuming! How can one find out which part of the code is consuming time? Could you point me to where I can learn how to do this?
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
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.

Sign in to comment.

More Answers (1)

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.

1 Comment

Thank you for clarifying, it's good to know that there is a specific error code for that.
It does seem like there is a problem currently with either MATLAB or the tests set up in this problem, I was getting the same problem in a similar problem titled "List the cuban primes" which was also made by the same person.
I feel that it is some of the commands/techniques used in the tests are whats causing the problems, as I am able to submit a solution to other problems, and submitting a solution with flawed syntax on the original problem immediately returns an error relevant to the syntax error.

Sign in to comment.

Products

Release

R2024b

Tags

Asked:

ty
on 3 Jun 2025

Edited:

on 4 Jun 2025

Community Treasure Hunt

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

Start Hunting!