Problem 659. How long is the longest prime diagonal?
Solution Stats
Problem Comments

9 Comments
I agree that the solution is miscoded. isprime(spiral(4)) gives:
1 0 0 0
0 0 1 1
1 0 1 0
0 0 0 1
which means that d=2 for this case.
Rescoring based on corrected test suite. Sorry about that.
"find the longest diagonal arrangement of primes in spiral(n)" leaves room for misinterpretation.
I would have quickly understood:
"return the length of the longest diagonal sequence of primes in spiral(n)"
Like Jonathan Campelli, I still feel the problem statement could be clearer. From the example I eventually figure that reference is being made to the diagonally oriented elements running between elements (1,5) and (4,2) of isprime(spiral(n)). But arguably these are not on a/the "diagonal" of that matrix, per http://mathworld.wolfram.com/Diagonal.html (cf. https://en.wikipedia.org/wiki/Main_diagonal ).
Furthermore, there is no mention in the current online MATLAB documentation that "spiral" is actually a function that is already provided with MATLAB — i.e. that the user does not have to write their own function to make the spiral matrix. Apparently, "spiral can be found in the demo folder" [ref: https://stackoverflow.com/questions/29466058/spirallooponamatrixfromapoint].
hard
eh, I think the problem statement made sense. especially if you just run 'isprime(spiral(n))' in your command window. I liked this problem. Ended up using the technique I almost used on another problem awhile back but it made more sense to use here, so I had the basic framework already started :D
for i=1:50
spy(isprime(spiral(i)))
pause(0.1)
end
Nice Problem
Solution Comments

1 Comment
Dear Bora,
You asked for help resolving this but I was "far away from Cody" for a long time... ;)
I celebrate you were at least successful with it.

1 Comment
Interesting! This error stems from using "Ix" in both function and subfunction. As I test my code in scripts not contained within a function I was missing it.
Why are parameters made available to subfunctions by default?

2 Comments
this is really awesome and impressive.I do learn something
What a wonderfull solution!!!

1 Comment
thank you very much

1 Comment
a good precise algorithm

2 Comments
Best solution that doesn't cheat with a lookup or regexp
Think I might have found a problem with merging the original and rotated matrices. In the case of n=7, if you change (1,6) and (2,7) to ones, the output becomes 6 when it should still be 4. If you evaluate the matrices separately then it should work fine, but apart from that this is a really nice solution

1 Comment
max(diff(find(~zeroOneVector)))1 returns the longest run of ones in a vector of zeros and ones, but it FAILS IN SEVERAL CASES, e.g.:
 all ones: [1 1 1 1 1] returns empty
 just a single zero: [1 1 0 1 1] returns empty
 all zeros at one end: [1 1 1 0 0] returns 0
 longest run at one end: [1 1 0 1 0] returns the longest run between zeros, here 1
As a workaround for all cases, add zeros to both ends of the vector:
max(diff(find(~[0 zeroOneVector 0])))1
However, this is not necessary for this solution because the matrix is very sparse.
Problem Recent Solvers376
Suggested Problems

Return the largest number that is adjacent to a zero
4698 Solvers

given 3 sides, find area of this triangle
775 Solvers

Project Euler: Problem 7, Nth prime
1129 Solvers

Project Euler: Problem 8, Find largest product in a large string of numbers
826 Solvers

235 Solvers
More from this Author50
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!