Cody

Problem 659. How long is the longest prime diagonal?

Solution 669857

Submitted on 13 May 2015 by Jonathan Campelli
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Pass
%% n = 4; d = 2; %p = isprime(spiral(n));imagesc(p) assert(isequal(prime_spiral(n),d))

d = 0 lgt = Empty matrix: 1-by-0 d = 1 0 lgt = 1 maximum = 0 1 d = 0 0 0 lgt = Empty matrix: 1-by-0 d = 1 0 1 1 lgt = 1 2 maximum = 0 1 0 2 d = 0 1 0 1 lgt = 1 1 maximum = 0 1 0 2 1 d = 0 1 0 0 lgt = 1 maximum = 0 1 0 2 1 1 d = 0 1 1 0 lgt = 2 maximum = 0 1 0 2 1 1 2 d = 0 1 0 0 lgt = 1 maximum = 0 1 0 2 1 1 2 1 d = 0 0 1 lgt = 1 maximum = 0 1 0 2 1 1 2 1 1 d = 0 0 lgt = Empty matrix: 1-by-0 d = 1 lgt = 1 maximum = 0 1 0 2 1 1 2 1 1 0 1 solution = 2

2   Pass
%% n = 7; d = 4; assert(isequal(prime_spiral(n),d))

d = 1 lgt = 1 maximum = 1 d = 0 0 lgt = Empty matrix: 1-by-0 d = 0 1 0 lgt = 1 maximum = 1 0 1 d = 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 1 1 1 0 0 lgt = 3 maximum = 1 0 1 0 3 d = 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 1 0 1 0 1 1 1 lgt = 1 1 3 maximum = 1 0 1 0 3 0 3 d = 0 0 0 1 0 0 1 lgt = 1 1 maximum = 1 0 1 0 3 0 3 1 d = 0 1 0 1 1 0 0 lgt = 1 2 maximum = 1 0 1 0 3 0 3 1 2 d = 0 0 0 0 1 1 0 lgt = 2 maximum = 1 0 1 0 3 0 3 1 2 2 d = 1 0 0 0 0 0 0 lgt = 1 maximum = 1 0 1 0 3 0 3 1 2 2 1 d = 0 0 0 1 1 0 0 lgt = 2 maximum = 1 0 1 0 3 0 3 1 2 2 1 2 d = 0 0 0 1 0 0 0 lgt = 1 maximum = 1 0 1 0 3 0 3 1 2 2 1 2 1 d = 0 0 0 0 1 1 1 lgt = 3 maximum = 1 0 1 0 3 0 3 1 2 2 1 2 1 3 d = 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 1 1 1 1 0 lgt = 4 maximum = 1 0 1 0 3 0 3 1 2 2 1 2 1 3 0 4 d = 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 1 lgt = 1 maximum = Columns 1 through 16 1 0 1 0 3 0 3 1 2 2 1 2 1 3 0 4 Columns 17 through 18 0 1 d = 0 0 lgt = Empty matrix: 1-by-0 d = 1 lgt = 1 maximum = Columns 1 through 16 1 0 1 0 3 0 3 1 2 2 1 2 1 3 0 4 Columns 17 through 20 0 1 0 1 solution = 4

3   Pass
%% n = 13; d = 5; assert(isequal(prime_spiral(n),d))

d = 0 lgt = Empty matrix: 1-by-0 d = 0 0 lgt = Empty matrix: 1-by-0 d = 0 1 0 lgt = 1 maximum = 0 0 1 d = 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 1 1 0 0 0 lgt = 2 maximum = 0 0 1 0 2 d = 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 1 0 1 1 0 1 1 lgt = 1 2 2 maximum = 0 0 1 0 2 0 2 d = 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 1 0 0 1 0 1 0 1 lgt = 1 1 1 1 maximum = 0 0 1 0 2 0 2 0 1 d = 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 1 1 1 1 1 0 0 1 0 0 lgt = 5 1 maximum = 0 0 1 0 2 0 2 0 1 0 5 d = 0 0 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 1 0 1 1 0 1 0 1 1 1 0 0 0 lgt = 1 2 1 3 maximum = 0 0 1 0 2 0 2 0 1 0 5 0 3 d = 0 0 0 0 0 0 1 0 0 0 0 0 0 lgt = 1 maximum = 0 0 1 0 2 0 2 0 1 0 5 0 3 1 d = 0 1 0 0 1 0 1 1 0 1 1 0 0 lgt = 1 1 2 2 maximum = 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 d = 0 0 0 0 0 0 0 0 0 0 1 0 0 lgt = 1 maximum = 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 d = 0 0 0 1 0 0 1 0 0 0 0 0 0 lgt = 1 1 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Column 17 1 d = 0 0 0 0 0 0 0 0 0 1 0 0 1 lgt = 1 1 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 18 1 1 d = 1 0 1 0 0 0 1 0 0 0 0 0 0 lgt = 1 1 1 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 19 1 1 1 d = 0 0 0 0 0 0 1 0 0 1 1 0 1 lgt = 1 2 1 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 20 1 1 1 2 d = 0 0 0 1 0 0 0 0 0 0 0 0 0 lgt = 1 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 21 1 1 1 2 1 d = 0 0 0 0 0 0 1 1 1 0 1 1 0 lgt = 3 2 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 22 1 1 1 2 1 3 d = 1 0 0 0 0 0 0 0 0 0 0 0 0 lgt = 1 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 23 1 1 1 2 1 3 1 d = 0 0 0 1 0 0 1 1 0 0 0 0 0 lgt = 1 2 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 24 1 1 1 2 1 3 1 2 d = 0 0 0 0 0 0 1 0 0 0 0 0 0 lgt = 1 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 25 1 1 1 2 1 3 1 2 1 d = 0 0 0 0 0 0 0 1 1 1 0 1 0 lgt = 3 1 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 26 1 1 1 2 1 3 1 2 1 3 d = 0 0 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 1 0 1 1 1 1 1 0 1 1 0 lgt = 1 5 2 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 28 1 1 1 2 1 3 1 2 1 3 0 5 d = 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 0 0 1 0 0 1 lgt = 1 1 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 30 1 1 1 2 1 3 1 2 1 3 0 5 0 1 d = 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 1 0 0 1 1 1 1 lgt = 1 4 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 32 1 1 1 2 1 3 1 2 1 3 0 5 0 1 0 4 d = 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 1 1 1 0 lgt = 3 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 32 1 1 1 2 1 3 1 2 1 3 0 5 0 1 0 4 Columns 33 through 34 0 3 d = 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 lgt = Empty matrix: 1-by-0 d = 1 lgt = 1 maximum = Columns 1 through 16 0 0 1 0 2 0 2 0 1 0 5 0 3 1 2 1 Columns 17 through 32 1 1 1 2 1 3 1 2 1 3 0 5 0 1 0 4 Columns 33 through 38 0 3 0 0 0 1 solution = 5

4   Pass
%% n = 52; d = 6; assert(isequal(prime_spiral(n),d))

d = 0 lgt = Empty matrix: 1-by-0 d = 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 lgt = Empty matrix: 1-by-0 d = 1 0 0 0 lgt = 1 maximum = 0 0 0 1 d = 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 0 0 1 lgt = 1 maximum = 0 0 0 1 0 1 d = 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 1 0 0 0 0 0 0 lgt = 1 maximum = 0 0 0 1 0 1 0 1 d = 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 1 0 0 0 0 1 0 0 0 lgt = 1 1 maximum = 0 0 0 1 0 1 0 1 0 1 d = 0 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 0 0 1 0 1 1 1 0 1 lgt = 1 3 1 maximum = 0 0 0 1 0 1 0 1 0 1 0 3 d = 0 0 0 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 0 0 0 0 0 0 1 0 0 0 0 lgt = 1 maximum = 0 0 0 1 0 1 0 1 0 1 0 3 0 1 d = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 0 0 0 1 1 0 1 1 0 0 1 0 1 lgt = 2 2 1 1 maximum = 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Column 17 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 Columns 17 through 18 0 1 lgt = 1 1 1 maximum = Columns 1 through 16 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 Columns 17 through 18 0 1 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 19 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 0 1 1 0 1 1 1 0 0 0 0 1 0 0 0 Columns 17 through 20 0 0 0 0 lgt = 2 3 1 maximum = Columns 1 through 16 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 Columns 17 through 20 0 1 0 3 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 21 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 1 0 0 1 1 0 1 1 0 0 0 0 1 1 0 0 Columns 17 through 22 1 0 0 1 0 1 lgt = 1 2 2 2 1 1 1 maximum = Columns 1 through 16 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 Columns 17 through 22 0 1 0 3 0 2 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 23 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 0 0 0 0 1 0 0 1 0 0 0 0 1 1 1 Columns 17 through 24 0 0 0 0 1 0 0 0 lgt = 1 1 3 1 maximum = Columns 1 through 16 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 Columns 17 through 24 0 1 0 3 0 2 0 3 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 25 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 0 1 0 0 0 0 1 0 1 0 1 1 0 0 1 Columns 17 through 26 0 0 1 0 0 0 0 0 1 0 lgt = 1 1 1 2 1 1 1 maximum = Columns 1 through 16 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 Columns 17 through 26 0 1 0 3 0 2 0 3 0 2 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 27 0 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 0 0 0 1 0 1 0 0 1 1 0 0 1 0 0 Columns 17 through 28 0 0 0 0 0 0 1 0 0 1 0 1 lgt = 1 1 2 1 1 1 1 maximum = Columns 1 through 16 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 Columns 17 through 28 0 1 0 3 0 2 0 3 0 2 0 2 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 29 0 0 0 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 Columns 17 through 30 1 0 0 0 0 1 1 1 0 0 0 0 1 0 lgt = 1 1 1 3 1 maximum = Columns 1 through 16 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 Columns 17 through 30 0 1 0 3 0 2 0 3 0 2 0 2 0 3 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 31 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 1 1 1 0 1 1 0 0 1 0 1 0 1 0 1 1 Columns 17 through 32 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 lgt = 3 2 1 1 1 2 1 1 1 maximum = Columns 1 through 16 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 Columns 17 through 32 0 1 0 3 0 2 0 3 0 2 0 2 0 3 0 3 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Column 33 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 Columns 17 through 32 0 0 0 1 0 0 0 0 0 1 0 1 1 0 1 0 Columns 33 through 34 0 1 lgt = 1 1 1 1 2 1 1 maximum = Columns 1 through 16 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 Columns 17 through 32 0 1 0 3 0 2 0 3 0 2 0 2 0 3 0 3 Columns 33 through 34 0 2 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 33 through 35 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 Columns 17 through 32 0 1 0 1 1 1 0 1 1 1 0 1 1 0 0 0 Columns 33 through 36 1 1 1 0 lgt = 1 1 1 3 3 2 3 maximum = Columns 1 through 16 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 Columns 17 through 32 0 1 0 3 0 2 0 3 0 2 0 2 0 3 0 3 Columns 33 through 36 0 2 0 3 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 33 through 37 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 Columns 17 through 32 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 33 through 38 0 1 0 0 0 0 lgt = 1 1 1 1 1 1 maximum = Columns 1 through 16 0 0 0 1 0 1 0 1 0 1 0 3 0 1 0 2 Columns 17 through 32 0 1 0 3 0 2 0 3 0 2 0 2 ...

5   Fail
%% n = 81; d = 9; assert(isequal(prime_spiral(n),d))

d = 0 lgt = Empty matrix: 1-by-0 d = 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 0 1 lgt = 1 maximum = 0 0 0 0 1 d = 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 1 1 1 0 0 0 lgt = 3 maximum = 0 0 0 0 1 0 3 d = 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 1 0 1 0 0 0 1 0 lgt = 1 1 1 maximum = 0 0 0 0 1 0 3 0 1 d = 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 1 0 0 0 0 0 0 0 0 lgt = 1 maximum = 0 0 0 0 1 0 3 0 1 0 1 d = 0 0 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 1 1 1 0 0 0 1 0 0 1 lgt = 3 1 1 maximum = 0 0 0 0 1 0 3 0 1 0 1 0 3 d = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 lgt = 1 1 1 maximum = 0 0 0 0 1 0 3 0 1 0 1 0 3 0 1 d = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Column 17 0 lgt = 1 maximum = Columns 1 through 16 0 0 0 0 1 0 3 0 1 0 1 0 3 0 1 0 Column 17 1 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 18 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 1 1 0 0 1 0 0 1 0 0 0 0 0 0 0 Columns 17 through 19 0 0 0 lgt = 2 1 1 maximum = Columns 1 through 16 0 0 0 0 1 0 3 0 1 0 1 0 3 0 1 0 Columns 17 through 19 1 0 2 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 20 0 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 1 0 0 0 1 0 0 1 0 1 0 0 1 0 0 0 Columns 17 through 21 0 0 0 1 0 lgt = 1 1 1 1 1 1 maximum = Columns 1 through 16 0 0 0 0 1 0 3 0 1 0 1 0 3 0 1 0 Columns 17 through 21 1 0 2 0 1 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 22 0 0 0 0 0 0 lgt = Empty matrix: 1-by-0 d = Columns 1 through 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 Columns 17 through 23 0 1 1 0 0 0 1 lgt = 2

Suggested Problems

More from this Author50

Community Treasure Hunt

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

Start Hunting!