local fmt = require "fmt" local int = require "int" require "table2" local can_follow = {} local arrang = {} local bfirst = true local function ptrs(res, n, done) local ad = arrang[done] if n - done <= 1 then if can_follow[ad][n] then if bfirst then fmt.tprint("%2d", arrang, #arrang) bfirst = false end res += 1 end else done += 1 for i = done, n - 1, 2 do local ai = arrang[i] if can_follow[ad][ai] then local aid = arrang[done] arrang[i], arrang[done] = aid, ai res = ptrs(res, n, done) arrang[i], arrang[done] = ai, aid end end end return res end local function prime_triangle(n) can_follow = table.create(n) for i = 1, n do can_follow[i] = table.rep(n, false) for j = 1, n do can_follow[i][j] = int.isprime(i + j) end end arrang = range(1, n) bfirst = true return ptrs(0, n, 1) end local res = {} for i = 2, 20 do res:insert(prime_triangle(i)) end print() print(res:concat(" "))