Last active
June 30, 2016 16:37
-
-
Save oxagast/42862af19f2b3cf138efa452a4f1f147 to your computer and use it in GitHub Desktop.
magic square
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
// __ _ _ __ ___ __ ____ ____ | |
// / ( \/ )/ _\ / __)/ _\/ ___(_ _) | |
// ( O ) (/ ( (_ / \___ \ )( | |
// \__(_/\_\_/\_/\___\_/\_(____/(__) | |
// magic square | |
#include <algorithm> | |
#include <cstdlib> | |
#include <cstring> | |
#include <iostream> | |
#include <math.h> | |
#include <stdio.h> | |
#include <string> | |
#include <unistd.h> | |
#include <vector> | |
int main() { | |
int width = 3; | |
int whole = pow(width, 2); | |
std::cout << "Width: " << width << std::endl | |
<< "Whole: " << whole << std::endl; | |
double sum = 0; | |
int start = 1; | |
for (start = 1; start <= whole; start++) { | |
sum = sum + start; | |
} | |
int magic = sum / width; | |
std::cout << "Summation: " << sum << std::endl | |
<< "Magic Number: " << magic << std::endl; | |
std::vector<int> table; | |
int r = width - 1; | |
std::vector<bool> choose(whole); | |
std::fill(choose.begin() + whole - r, choose.end(), true); | |
int curper, lastper, othernum; | |
do { | |
for (int i = 0; i < whole; ++i) { | |
lastper = curper; | |
if (choose[i]) { | |
// std::cout << (i+1) << " "; | |
curper = i + 1; | |
} | |
othernum = magic - (curper + lastper); | |
if ((othernum <= whole) && (lastper <= whole) && (curper <= whole)) { | |
if ((othernum != curper) && (othernum != lastper) && | |
(lastper != curper)) { | |
if ((othernum > 0) && (lastper > 0) && (curper > 0)) { | |
if ((othernum + curper + lastper) == magic) { | |
table.push_back(curper); | |
table.push_back(lastper); | |
table.push_back(othernum); | |
} | |
} | |
} | |
} | |
} | |
} while (std::next_permutation(choose.begin(), choose.end())); | |
for (int h = 0; h < table.size(); h = h + 3) { | |
for (int v = 0; v < table.size(); v = v + 3) { | |
for (int d = 0; d < table.size(); d = d + 3) { | |
if (((table[h] + table[h + 1] + table[h + 2]) == magic) && | |
((table[h] + table[v + 1] + table[d + 2]) == magic) && | |
((table[h + 2] + table[v + 1] + table[d]) == magic) && | |
((table[d + 2] + table[v + 1] + table[h]) == magic) && | |
((table[d] + table[d + 1] + table[d + 2]) == magic) && | |
((table[v] + table[v + 1] + table[v + 2]) == magic) && | |
((table[h] + table[v] + table[d]) == magic) && | |
((table[h + 1] + table[v + 1] + table[d + 1]) == magic) && | |
((table[h + 2] + table[v + 2] + table[d + 2]) == magic)) { | |
std::cout << table[h] << table[h + 1] << table[h + 2] << std::endl | |
<< table[v] << table[v + 1] << table[v + 2] << std::endl | |
<< table[d] << table[d + 1] << table[d + 2] << std::endl | |
<< std::endl; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment