@Zoby, this time it is my program you should try to analyse. It isn't that hard but I hope you have some fun with it. As small hint: it is related to a mathematician who participated in an infamous project.
#include <stdio.h>
#define p(i,t,x)for(i=t;i<x;i++)
main(){int m=0400,i,j,r,u[4*(m*m+m
)+1],p=2*(m+m*m) ,t=0,l[]={2,0,1,1
,1},q[4*(m* m+m)+1],d[]=
{1,-m*2- 1,-1,m*2+1
,1},x;u [p]=t++;p(j,
0,4 *(m*m+m)+1)q[j
]=j<2?0:1;p(j,
2,4*(m*m+m)+1)
{x=j;while((x
+=j)<4*(m*m+
m)+1)q[x]=0;}
p(i,1,m+1)p(r,
0,5){l[r]+=r<
1?0:2;p(j,1,l[
r])u[p+=d[r]]=t++;
}printf("P%d\n%u"\
" %u\n%d\n",2|
1,(m*2
+1),(m
*2+1),
0xff);
p(j,0,
4*(m*m+m)+1)
printf("0 %u"\
" 0 ",q[u[j]]*
128);}
Use gcc test.c -o test && ./test > test.ppm
to generate an image.
Update: Of course, it generates the Ulam spiral. It starts by generating an array with prims using the sieve of Eratosthenes (0 == not a prime, 0 != a prime) and a translation array to map positions inside a 513x513 sized image of a spiral to its linear offset of the unrolled spiral. The last step is to print the informations to stdout.