Problem 659. How long is the longest prime diagonal?
Stanislaw Ulam once observed that if the counting numbers are arranged in a spiral, the prime numbers contained in it form a surprising pattern. They appear to cluster along diagonals of the spiral matrix.
Given n, return the length of the longest diagonal sequence of primes in spiral(n).
Example:
Input n = 7
Output d = 4
Since isprime(spiral(n)) is
1 0 0 0 1 0 0
0 0 0 1 0 0 0
1 0 1 0 0 0 0
0 1 0 0 1 1 0
0 0 1 0 1 0 1
0 1 0 0 0 1 0
1 0 0 0 0 0 1
Solution Stats
Problem Comments
-
9 Comments
Show
6 older comments
Highphi
on 24 Jul 2020
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
mahesh kumar
on 8 Nov 2020
for i=1:50
spy(isprime(spiral(i)))
pause(0.1)
end
Dara Murray
on 19 Jul 2021
Nice Problem
Solution Comments
Show commentsProblem Recent Solvers398
Suggested Problems
-
Renaming a field in a structure array
1548 Solvers
-
Least common multiple of many numbers
238 Solvers
-
303 Solvers
-
Make a random, non-repeating vector.
9687 Solvers
-
369 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!