Cody

# Problem 659. How long is the longest prime diagonal?

Solution 551504

Submitted on 2 Jan 2015 by Philip
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))

z = Columns 1 through 16 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 17 through 32 0 1 0 0 0 0 1 1 0 0 0 0 0 0 0 0 Columns 33 through 48 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 Columns 49 through 64 0 0 1 0 1 1 0 0 0 0 0 0 0 0 1 0 Columns 65 through 70 0 0 0 0 0 0

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

z = Columns 1 through 16 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 17 through 32 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 33 through 48 0 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 Columns 49 through 64 1 1 1 0 0 0 0 0 0 0 0 0 1 0 0 0 Columns 65 through 80 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 Columns 81 through 96 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 Columns 97 through 112 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 Columns 113 through 128 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 Columns 129 through 144 0 0 0 0 0 0 0 0 0 1 0 1 1 0 0 0 Columns 145 through 160 0 0 0 1 0 0 0 0 1 0 1 0 1 1 1 0 Columns 161 through 176 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 Columns 177 through 192 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 Columns 193 through 208 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0

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

z = Columns 1 through 16 1 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 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 49 through 64 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 Columns 65 through 80 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 81 through 96 0 0 0 0 1 1 1 1 0 0 1 0 0 0 0 0 Columns 97 through 112 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 113 through 128 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 Columns 129 through 144 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 Columns 145 through 160 1 1 1 1 1 0 1 0 0 0 0 0 0 0 0 0 Columns 161 through 176 0 0 0 0 0 0 0 0 0 1 0 1 1 1 0 0 Columns 177 through 192 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 Columns 193 through 208 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 Columns 209 through 224 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 225 through 240 0 0 0 0 0 1 1 0 1 1 1 0 0 0 0 0 Columns 241 through 256 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 257 through 272 0 0 1 0 1 1 0 0 1 0 0 0 0 0 0 0 Columns 273 through 288 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 289 through 304 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 Columns 305 through 320 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 321 through 336 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 337 through 352 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 353 through 368 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 369 through 384 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 385 through 400 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 401 through 416 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 Columns 417 through 432 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 433 through 448 0 0 1 0 1 0 0 0 1 0 0 0 0 0 0 0 Columns 449 through 464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 465 through 480 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 Columns 481 through 496 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 Columns 497 through 512 1 1 0 1 1 0 0 0 0 0 0 0 0 0 1 0 Columns 513 through 528 0 0 0 0 0 0 1 0 1 1 0 1 0 1 1 1 Columns 529 through 544 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 545 through 560 0 0 0 0 0 1 1 1 1 1 0 0 1 0 0 0 Columns 561 through 576 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 577 through 592 0 0 0 1 0 0 1 0 1 0 1 0 0 0 0 0 Columns 593 through 608 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 609 through 624 1 0 1 1 0 1 1 0 0 0 0 0 0 0 0 0 Columns 625 through 640 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 Columns 641 through 656 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 657 through 672 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 Columns 673 through 688 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 689 through 700 0 0 0 0 0 0 0 0 0 0 0 0

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

z = Columns 1 through 16 1 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 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 49 through 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 65 through 80 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 81 through 96 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 97 through 112 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 113 through 128 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 129 through 144 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 145 through 160 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 161 through 176 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 177 through 192 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 193 through 208 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 209 through 224 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 Columns 225 through 240 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 241 through 256 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 257 through 272 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 273 through 288 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 289 through 304 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 305 through 320 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 321 through 336 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 Columns 337 through 352 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 353 through 368 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 369 through 384 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 385 through 400 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 401 through 416 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 417 through 432 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 Columns 433 through 448 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 449 through 464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 465 through 480 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 481 through 496 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 497 through 512 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 513 through 528 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 529 through 544 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 545 through 560 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 561 through 576 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 577 through 592 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 593 through 608 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 609 through 624 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 625 through 640 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 Columns 641 through 656 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 Columns 657 through 672 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 673 through 688 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 689 through 704 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 705 through 720 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 721 through 736 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 737 through 752 0 0 0 0 0 0 0 1 1 1 1 1 1 0 0 0 Columns 753 through 768 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 769 through 784 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 785 through 800 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 801 through 816 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 817 through 832 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 833 through 848 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 849 through 864 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 Columns 865 through 880 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 881 through 896 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 897 through 912 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 913 through 928 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 929 through 944 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 945 through 960 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 961 through 976 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 0 Columns 977 through 992 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 993 through 1008 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1009 through 1024 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1025 through 1040 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1041 through 1056 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1057 through 1072 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 Columns 1073 through 1088 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 Columns 1089 through 1104 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1105 through 1120 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1121 through 1136 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1137 through 1152 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1153 through 1168 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 Columns 1169 through 1184 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 Columns 1185 through 1200 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1201 through 1216 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1217 through 1232 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1233 through 1248 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1249 through 1264 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1265 through 1280 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 0 Columns 1281 through 1296 0 1 0 1 0 0 0 1 0 0 0 0 1 1 0 0 Columns...

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

z = Columns 1 through 16 1 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 48 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 49 through 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 65 through 80 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 81 through 96 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 97 through 112 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 113 through 128 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 129 through 144 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 145 through 160 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 161 through 176 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 Columns 177 through 192 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 193 through 208 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 209 through 224 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 225 through 240 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 241 through 256 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 257 through 272 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 273 through 288 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 289 through 304 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 305 through 320 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 321 through 336 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 337 through 352 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 353 through 368 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 369 through 384 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 385 through 400 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 401 through 416 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 417 through 432 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 433 through 448 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 449 through 464 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 465 through 480 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 481 through 496 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 Columns 497 through 512 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 513 through 528 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 529 through 544 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 545 through 560 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 561 through 576 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 577 through 592 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 593 through 608 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 609 through 624 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 625 through 640 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 641 through 656 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 657 through 672 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 Columns 673 through 688 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 689 through 704 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 705 through 720 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 721 through 736 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 737 through 752 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 753 through 768 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 769 through 784 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 785 through 800 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 801 through 816 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 817 through 832 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 Columns 833 through 848 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 849 through 864 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 865 through 880 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 881 through 896 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 897 through 912 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 913 through 928 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 929 through 944 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 945 through 960 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 961 through 976 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 977 through 992 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1 Columns 993 through 1008 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1009 through 1024 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1025 through 1040 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1041 through 1056 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1057 through 1072 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1073 through 1088 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1089 through 1104 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1105 through 1120 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1121 through 1136 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1137 through 1152 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 Columns 1153 through 1168 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1169 through 1184 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1185 through 1200 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1201 through 1216 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1217 through 1232 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1233 through 1248 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1249 through 1264 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1265 through 1280 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns 1281 through 1296 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Columns...