/* determine minimal prime 3x3 magic square; for more details see bottom */ #include #include #include uint32_t B[]={0x35145105,0x4510414,0x11411040,0x45144001}; #define Prime(i) ((B[(i)>>5] & (0x80000000UL >> ((i)%32))) != 0) #define forall_odd_primes_less_than(p, m, block) \ for((p)=3; (p)<(m); (p)+=2) \ if (Prime((p))) \ block uint8_t p,a,b,c,d; struct timeval tv0,tv1; int main(void) { gettimeofday(&tv1, NULL); // wait for usec change do gettimeofday(&tv0, NULL); while (tv0.tv_usec == tv1.tv_usec); forall_odd_primes_less_than(p, 64, forall_odd_primes_less_than(a, p, if Prime(2*p-a) { forall_odd_primes_less_than(b, p, if ( (b!=a) && Prime(2*p-b) ) { c= 3*p - (a+b); if ( (c<2*p) && (2*p-c!=a) && (2*p-c!=b) && Prime(c) && Prime(2*p-c) ) { if (2*a+b>2*p) { d = 2*a + b - 2*p; // 3*p - (3*p-(a+b)) - (2*p-a) if ( (d!=a) && (d!=b) && (d!=2*p-c) && Prime(d) && Prime(2*p-d) ) { gettimeofday(&tv1, NULL); printf("%3u|%3u|%3u|\n%3u|%3u|%3u|\n%3u|%3u|%3u|\n", a,b,c,2*p-d,p,d,2*p-c,2*p-b,2*p-a); printf("\n%ldus\n", 1000000*(tv1.tv_sec-tv0.tv_sec)+tv1.tv_usec-tv0.tv_usec); return 0; } } } } ) } ) ) } /* it always exists this by rotation and flippings (= is p, -/+ is less/greater p) --? ?=? ??? proof by enumeration of all possibilities ++ = -- +- +-+ +-+ = = +=- +- -+- -+- +-+ -= -+- +-- = +- -+ -+ -++ = +=- +=- -+ -+ --+ -+- +=- -+ -+ -= -+ -- = ++ row/column/diagonal sum is 3*p a b 3*p-(a+b)=c - - + p 2*a+b-2*p=d + = - 2*p-a - + + */