Last active
August 24, 2019 00:24
-
-
Save PatrickChildersIT/330c57d33c30ad405e46eca353ec9070 to your computer and use it in GitHub Desktop.
cpp version of the python program to randomly (attempt to) generate magic squares of squared numbers. 4.7 million/s
This file contains hidden or 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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <locale.h> | |
| #include <stdbool.h> | |
| #include <cstring> | |
| #include <iomanip> | |
| #include <iostream> | |
| #include <string> | |
| #include <random> | |
| #include <ctime> | |
| #include <iterator> | |
| std::string with_commas(long long value) { | |
| std::string numWithCommas = std::to_string(value); | |
| int insertPosition = numWithCommas.length() - 3; | |
| while (insertPosition > 0) { | |
| numWithCommas.insert(insertPosition, ","); | |
| insertPosition -= 3; | |
| } | |
| return numWithCommas; | |
| } | |
| void print_square(int* magic_square) { | |
| int loop; | |
| for (loop = 0; loop < 9; loop++) { | |
| printf("%d ", magic_square[loop]); | |
| } | |
| printf("\n"); | |
| } | |
| bool compute_square(int* numbers) { | |
| int magic = numbers[0] + numbers[1] + numbers[2]; | |
| if ((numbers[3] + numbers[4] + numbers[5]) != magic) { | |
| return false; | |
| } | |
| if ((numbers[6] + numbers[7] + numbers[8]) != magic) { | |
| return false; | |
| } | |
| if ((numbers[0] + numbers[3] + numbers[6]) != magic) { | |
| return false; | |
| } | |
| if ((numbers[1] + numbers[4] + numbers[7]) != magic) { | |
| return false; | |
| } | |
| if ((numbers[2] + numbers[5] + numbers[8]) != magic) { | |
| return false; | |
| } | |
| printf("all rows and columns:\n\t"); | |
| print_square(numbers); | |
| if ((numbers[0] + numbers[4] + numbers[8]) != magic) { | |
| return false; | |
| } | |
| printf("all rows, all columns, and one diagonal!!\n\t"); | |
| print_square(numbers); | |
| if ((numbers[2] + numbers[4] + numbers[6]) != magic) { | |
| return false; | |
| } | |
| return true; | |
| } | |
| void populate_squared_numbers(int* all_squared_numbers) { | |
| int index; | |
| for (index = 1; index < 101; index++) { | |
| all_squared_numbers[index-1] = index * index; | |
| } | |
| } | |
| int main() { | |
| long long square_count = 0; | |
| int all_squared_numbers[100]; | |
| std::fill(all_squared_numbers, all_squared_numbers + 100, 0); | |
| populate_squared_numbers(all_squared_numbers); | |
| int index; | |
| while (true) { | |
| std::random_shuffle(&all_squared_numbers[0], &all_squared_numbers[91]); | |
| for (index = 0; index < 91; index++) { | |
| if (square_count % 1000000 == 0) { | |
| std::cout << with_commas(square_count) << std::endl; | |
| } | |
| if (compute_square(&all_squared_numbers[index])) { | |
| printf("FOUND "); | |
| print_square(&all_squared_numbers[index]); | |
| std::cin.get(); | |
| std::cin.get(); | |
| return 0; | |
| } | |
| square_count++; | |
| } | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment