I wrote & timed a bunch of sieve type algorithms until I realized the problem was much simpler. Interesting problem.
For all of those having "server error", your code takes too much time on last test. Hint: you don't need to evaluate all numbers between n_min & n_max
why is my solution failing? it runs fine in a separate window of Matlab.
it looks like it is just the output isn't in the right format
i don't know why my "-1" is in a new line.
The error you are receiving has to do with the Cody server, not your function. Try it again (wait a few hours between attempts) until you no longer see the "temporary unavailability of MATLAB Service" error.
why are all they answers wrong? the first 5 prime numbers between 60 and 300 are the same for 60 and 1000.
Could you please comment on my solution? I see the size is much larger than leading solution, but regardless. I would say my solution is lame, because it was taylored for this specific set of conditions (test suite), I would like to make "universal" solution, which could work for random inputs, but this taylored solution seems much easier. What do you think?
Thank you for your opinion :)
Try setting up a loop to test individual values using isprime() that will terminate upon reaching the fifth prime number to prevent an inordinately long loop.
very nice problem!
Yes, finally a notorious prime problem! It was so satisfying to see the large range test cases pass. As hinted by someone, the trick is to avoid unnecessary computation, be it computing the primes or checking for 5 in the digits. I recommend careful timing of each code segment.
I always get the same error :
While evaluating the solution, the server encountered an error caused by temporary unavailability of MATLAB Service. Wait a few minutes for the MATLAB Service to return, and then rescore.
I copied the tests into a script on my personal MATLAB to time the execution of all the tests. My PC is nothing too flashy...and the script finished in 2.4 seconds with no discrepancies. I am using what I consider to be an 'efficient method' to solve this problem. However the server continually gives me the same error for 'lacking necessary MATLAB service,' which I assume means the testing process is taking too long?
In my own conscience I have solved the problem, but I'll never be able to complete the easy cody challenge at this rate. :'(
Elliot, we noticed this odd error on Monday. I thought that it had been fixed, so give this a try again. That error has nothing to do with your solution.
What is the correct answer for the 12nd test?? I could only put a if to avoid it...
This solution is ~500 times faster than my previous solution.
Gotta give up, two perfectly working solutions when run in Matlab consistently produce errors:
While evaluating the solution, the server encountered an error caused by long running MATLAB code. Edit the code if needed and then submit.
Not my problem, it's the server that has a problem running simple (and fast) code!
your solution takes a VERY long time to solve problem 7 in the testsuite (on my computer this takes around 3 minutes; Cody has a timeout of approximately 30 seconds). Problem 7 has a ridiculously high n_max value, I imagine precisely to encourage players to go beyond pre-computing-all-primes-within-range-type of solutions...
The Matlab server returns an error if you try and generate a sequence of numbers using any variant of:
x = n_min:n_max;
While vectorized solutions are usually preferable, that test case obviously fails in that respect because of the very large number of values within the range. For this problem, try using isprime(), rather than primes(), and set up a loop to test individual values and that will terminate upon reaching the fifth candidate number, to prevent an inordinately long loop.
[ans,n] is a nice idiom (maybe not in production code, for cody). I like it!
Find the treasures in MATLAB Central and discover how the community can help you!
Select a Web
Choose a web site to get translated content where available and see local events and
offers. Based on
your location, we recommend that you select: .
You can also select a web site from the following list:
Select the China site (in Chinese or English) for best site performance. Other MathWorks country
sites are not optimized for visits from your location.
Contact your local office