Last active
March 23, 2017 09:31
-
-
Save youxkei/2cea69a5174c6419b0686079a88c83a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std.stdio : writeln; | |
import std.range : iota; | |
import std.array : array; | |
import std.algorithm : fold, nextPermutation, all, sort; | |
import std.typecons : tuple, Tuple; | |
import std.parallelism : parallel; | |
enum MAX = 20; | |
bool isPrime(in int n){ | |
switch (n) | |
{ | |
case 0: | |
case 1: return false; | |
case 2: | |
case 3: return true; | |
default: break; | |
} | |
if(n % 2 == 0) return false; | |
if(n % 3 == 0) return false; | |
if(n % 6 != 1 && n % 6 != 5) return false; | |
for (int i = 5; i * i <= n; i += 6) | |
{ | |
if(n % i == 0) return false; | |
if(n % (i + 2) == 0) return false; | |
} | |
return true; | |
} | |
void main() | |
{ | |
foreach (m; iota(3, MAX + 1).parallel()) | |
{ | |
bool[Tuple!(immutable(int)[], immutable(int)[])] occured; | |
int[] seq = 2.iota(m + 1).array(); | |
permutation: do { | |
foreach (i; 0 .. seq.length - 2) | |
{ | |
immutable(int)[] filteredSeq = seq[0 .. i].dup.sort().array(); | |
if (!filteredSeq.all!isPrime) continue permutation; | |
int[] remainedSeq = seq[i .. $].dup; | |
foreach (j; 1 .. remainedSeq.length - 1) | |
{ | |
immutable(int)[] lhs = remainedSeq[0 .. j].dup.sort().array(); | |
immutable(int)[] rhs = remainedSeq[j .. $].dup.sort().array(); | |
if (lhs.fold!"a*b"(1) == rhs.fold!"a*b"(1)) | |
{ | |
if (tuple(filteredSeq, lhs) !in occured && tuple(filteredSeq, rhs) !in occured) | |
{ | |
writeln(m, " ", seq[0 .. i], " ", lhs, " ", rhs); | |
occured[tuple(filteredSeq, lhs)] = true; | |
occured[tuple(filteredSeq, rhs)] = true; | |
} | |
} | |
} | |
} | |
} while (seq.nextPermutation()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment