Your function will be given lower and upper integer bounds. Your task is to return a vector containing the first five prime numbers in that range that contain the number five. But, if you can't find at least five such numbers, the function should give up and return -1.
For example, for n_min = 60 and n_max = 1000, the set of prime numbers is:
p = [61,67,71,73,79, ... 149,151,157,163, ... 241,251,257,263, ... 349,353,359,367, ... 983,991,997]
This set contains at least five numbers that contain a five; the first five are:
p5 = [151,157,251,257,353]
which is the set that your function should return in this case. If, however, n_max were set at 300, five such numbers do not exist and the function should then give up (return -1).
I wrote & timed a bunch of sieve type algorithms until I realized the problem was much simpler. Interesting problem.
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
example
mine says
y_correct=
-1
templates wants
y_correct= -1
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.
Hey ;)
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 :
Info
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!
Remove the polynomials that have positive real elements of their roots.
439 Solvers
388 Solvers
942 Solvers
Reverse the elements of an array
505 Solvers
205 Solvers