Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace MagicPrimeSquare
- {
- class Program
- {
- static void Main()
- {
- bool[] isPrime = new bool[480];
- isPrime[2] = true;
- for (int i = 2; i < 480; i++)
- {
- isPrime[i] = true;
- }
- for (int i = 2; i < 480; i++)
- {
- if (isPrime[i])
- {
- for (int j = 2 * i; j < 480; j += i)
- {
- isPrime[j] = false;
- }
- }
- }
- var oddPrimesUpTo480 = new List<int>();
- for (int idx = 3; idx < 480; idx++)
- {
- if (isPrime[idx])
- {
- oddPrimesUpTo480.Add(idx);
- }
- }
- // Magic square:
- // a b c
- // d e f
- // g h i
- //
- // Sums: S1 = a + b + c, S2 = d + e + f, S3 = g + h + i, S4 = a + d + g, S5 = b + e + h, S6 = c + f + i, S7 = c + e + g, S8 = a + e + i
- var recordSum = 366;
- foreach (var a in oddPrimesUpTo480)
- {
- foreach (var b in oddPrimesUpTo480)
- {
- if (b == a) continue;
- foreach (var c in oddPrimesUpTo480)
- {
- // To show progress
- // Console.WriteLine("a b c = {0} {1} {2}", a, b, c);
- var s1 = a + b + c;
- if (!oddPrimesUpTo480.Contains(s1)) continue;
- if ((s1 == a) || (s1 == b)) continue;
- if (s1 >= recordSum) continue;
- if ((c == s1) || (c == b) || (c == a)) continue;
- foreach (var d in oddPrimesUpTo480)
- {
- if ((d == s1) || (d == c) || (d == b) || (d == a)) continue;
- foreach (var e in oddPrimesUpTo480)
- {
- if ((e == s1) || (e == d) || (e == c) || (e == b) || (e == a)) continue;
- foreach (var f in oddPrimesUpTo480)
- {
- var s2 = d + e + f;
- if (!oddPrimesUpTo480.Contains(s2)) continue;
- if ((s2 == a) || (s2 == b) || (s2 == c) || (s2 == d) || (s2 == e) || (s2 == f) || (s2 == s1)) continue;
- if (s1 + s2 >= recordSum) continue;
- if ((f == s1) || (f == s2) || (f == e) || (f == d) || (f == c) || (f == b) || (f == a)) continue;
- foreach (var g in oddPrimesUpTo480)
- {
- var s4 = a + d + g;
- if (!oddPrimesUpTo480.Contains(s4)) continue;
- if (s1 + s2 + s4 >= recordSum) continue;
- if ((s4 == a) || (s4 == b) || (s4 == c) || (s4 == d) || (s4 == e) || (s4 == f) || (s4 == g) || (s4 == s1) || (s4 == s2)) continue;
- var s7 = c + e + g;
- if (!oddPrimesUpTo480.Contains(s7)) continue;
- if (s1 + s2 + s4 + s7 >= recordSum) continue;
- if ((s7 == a) || (s7 == b) || (s7 == c) || (s7 == d) || (s7 == e) || (s7 == f) || (s7 == g) || (s7 == s1) || (s7 == s2) || (s7 == s4)) continue;
- if ((g == s1) || (g == s2) || (g == s4) || (g == s7) || (g == f) || (g == e) || (g == d) || (g == c) || (g == b) || (g == a)) continue;
- foreach (var h in oddPrimesUpTo480)
- {
- var s5 = b + e + h;
- if (!oddPrimesUpTo480.Contains(s5)) continue;
- if (s1 + s2 + s4 + s7 + s5 >= recordSum) continue;
- if ((s5 == a) || (s5 == b) || (s5 == c) || (s5 == d) || (s5 == e) || (s5 == f) || (s5 == g) || (s5 == h) || (s5 == s1) || (s5 == s2) || (s5 == s4) || (s5 == s7)) continue;
- if ((h == s1) || (h == s2) || (h == s4) || (h == s7) || (h == s5) || (h == g) || (h == f) || (h == e) || (h == d) || (h == c) || (h == b) || (h == a)) continue;
- foreach (var i in oddPrimesUpTo480)
- {
- if ((i == s1) || (i == s2) || (i == s4) || (i == s7) || (i == s5) || (i == h) || (i == g) || (i == f) || (i == e) || (i == d) || (i == c) || (i == b) || (i == a)) continue;
- var s3 = g + h + i;
- if (!oddPrimesUpTo480.Contains(s3)) continue;
- if (s1 + s2 + s4 + s7 + s5 + s3 >= recordSum) continue;
- if ((s3 == a) || (s3 == b) || (s3 == c) || (s3 == d) || (s3 == e) || (s3 == f) || (s3 == g) || (s3 == h) || (s3 == i) || (s3 == s1) || (s3 == s2) || (s3 == s4) || (s3 == s7) || (s3 == s5)) continue;
- var s6 = c + f + i;
- if (!oddPrimesUpTo480.Contains(s6)) continue;
- if (s1 + s2 + s4 + s7 + s5 + s3 + s6 >= recordSum) continue;
- if ((s6 == a) || (s6 == b) || (s6 == c) || (s6 == d) || (s6 == e) || (s6 == f) || (s6 == g) || (s6 == h) || (s6 == i) || (s6 == s1) || (s6 == s2) || (s6 == s4) || (s6 == s7) || (s6 == s5) || (s6 == s3)) continue;
- var s8 = a + e + i;
- if (!oddPrimesUpTo480.Contains(s8)) continue;
- if ((s8 == a) || (s8 == b) || (s8 == c) || (s8 == d) || (s8 == e) || (s8 == f) || (s8 == g) || (s8 == h) || (s8 == i) || (s8 == s1) || (s8 == s2) || (s8 == s4) || (s8 == s7) || (s8 == s5) || (s8 == s3) || (s8 == s6)) continue;
- var totalsum = s1 + s2 + s3 + s4 + s5 + s6 + s7 + s8;
- if (totalsum < recordSum)
- {
- recordSum = totalsum;
- Console.WriteLine("---");
- Console.WriteLine("Sum of {0}:", totalsum);
- Console.WriteLine(" {0} {1} {2}|{3}", a, b, c, s1);
- Console.WriteLine(" {0} {1} {2}|{3}", d, e, f, s2);
- Console.WriteLine(" {0} {1} {2}|{3}", g, h, i, s3);
- Console.WriteLine(" -----------|---");
- Console.WriteLine("{0} {1} {2} {3}|{4}", s7, s4, s5, s6, s8);
- // If progress is showed, use this so you won't miss any solutions
- // Console.ReadKey();
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement