Skip to content

Instantly share code, notes, and snippets.

@Hermann-SW
Last active July 5, 2022 18:41
Show Gist options
  • Save Hermann-SW/6772285cc8a3ac2a347a89543d9d3dbe to your computer and use it in GitHub Desktop.
Save Hermann-SW/6772285cc8a3ac2a347a89543d9d3dbe to your computer and use it in GitHub Desktop.
q32.c adapted for Raspberry Pico
/* determine minimal prime 3x3 magic square; for more details see bottom */
#include <stdio.h>
#include "pico/stdlib.h"
#include <sys/time.h>
#include <stdint.h>
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;
uint32_t t0,t1;
int main(void)
{
stdio_init_all();
//set_sys_clock_khz(275000, true); // /dev/ttyACM0 missing
//set_sys_clock_khz(124400, true); // /dev/ttyACM0 missing
// 330us
//set_sys_clock_khz(124000, true); // 346us
//set_sys_clock_khz(126000, true); // 340us
//set_sys_clock_khz(128000, true); // 335us
//set_sys_clock_khz(130000, true); // 330us
//set_sys_clock_khz(131000, true); // 328us
//set_sys_clock_48mhz(); // 894us
//set_sys_clock_khz(48000, true); // 932us
//set_sys_clock_khz(50000, true); // 894us
//set_sys_clock_khz(200000, true); // 214us
//set_sys_clock_khz(250000, true); // 171us
//set_sys_clock_khz(266000, true); // 161us (1596/6)
//set_sys_clock_khz(270000, true); // 158us
sleep_ms(5000);
t1 = time_us_32(); // wait for usec change
do t0 = time_us_32(); while (t0 == t1);
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) )
{
t1 = time_us_32();
printf("\n%ldus\n",
t1 - t0);
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",
t1 - t0);
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 - + +
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment