Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- # Filename: magic_square_generator.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- - This script generates magic squares using the Siamese method, where all rows, columns, and diagonals sum to the same value.
- - The Siamese method initializes by placing the number 1 in the middle column of the top row.
- - Subsequent numbers are then positioned diagonally up and right from the previous number, wrapping around to the last row or first column if necessary.
- - If a cell is already occupied, the next number is placed one row down from the current cell.
- - Padding is applied to ensure uniform appearance across different square dimensions.
- - As the size of the square increases, padding is added to numbers to maintain a consistent number of digits for aesthetic purposes.
- - The algorithm specifically supports odd dimensions for the magic square, as even dimensions are incompatible with the Siamese method.
- Parameters:
- - n (int): The size of the magic square (n x n).
- Padding:
- * Padding is applied to ensure consistent digit representation in the generated magic squares.
- - For dimensions 3x3 to 9x9, 1-digit numbers are padded with leading zeros to have 2 digits.
- - For dimensions 11x11 to 31x31, 2-digit numbers are padded with leading zeros to have 3 digits.
- - For dimensions 33x33 to 99x99, 3-digit numbers are padded with leading zeros to have 4 digits.
- - For dimensions 101x101 to 315x315, 4-digit numbers are padded with leading zeros to have 5 digits.
- - For dimensions 317x317 to 999x999, 5-digit numbers are padded with leading zeros to have 6 digits.
- - For dimensions 1001x1001 to Infinity, 6-digit numbers are padded with leading zeros to have 7 digits.
- * Please note that higher dimensions containing 7-digit values are not padded further.
- Requirements:
- - Python3.x
- Functions:
- - magic_square(n): Generates a magic square of size n x n.
- Usage:
- - Run the script in a Python environment.
- - Follow the on-screen prompts to enter an odd number greater than 1 as the size of the magic square.
- Example Expected Output:
- ------------------------------------------------------------------------------
- :: MAGIC SQUARE GENERATOR ::
- Enter an odd number greater than 1 as the size of the Magic Square (n x n): 5
- ┌────────────────┐
- │ 09 03 22 16 15 │
- │ 02 21 20 14 08 │
- │ 25 19 13 07 01 │
- │ 18 12 06 05 24 │
- │ 11 10 04 23 17 │
- └────────────────┘
- The sum of each row/column/diagonal is: 65.0
- ------------------------------------------------------------------------------
- Additional Notes:
- - This algorithm only supports odd dimensions for the magic square.
- - Magic squares for dimensions less than or equal to 1 are not allowed.
- - Dimensions exceeding 8-digit values are not padded further.
- """
- # Function to create Magic Square
- def magic_square(n):
- if n < 3:
- print(
- "- Magic squares for dimensions less than 3 are not supported by this algorithm!\n"
- )
- return
- elif n % 2 == 0:
- print("- Even dimension values are not supported by this algorithm!\n")
- return
- # Initialize the magic square with zeros
- magicSquare = []
- for i in range(n):
- listt = []
- for j in range(n):
- listt.append(0)
- magicSquare.append(listt)
- # Initialize starting position
- i = n // 2
- j = n - 1
- # Set the number of elements to insert
- num = n * n
- count = 1
- # Populate the magic square
- while count <= num:
- if i == -1 and j == n: # condition 4
- j = n - 2
- i = 0
- else:
- if j == n: # column value is exceeding
- j = 0
- if i < 0: # row is becoming -1
- i = n - 1
- # Check if cell is occupied
- if magicSquare[i][j] != 0:
- j = j - 2
- i = i + 1
- continue
- else:
- magicSquare[i][j] = count
- count += 1
- # Move to the next cell
- i = i - 1
- j = j + 1 # condition 1
- # Print top border
- if n >= 1001:
- print("\t\t\t┌" + "─" * (8 * n + 1) + "┐") # 1001x1001 - Infinity (not padded further)
- elif n >= 317:
- print("\t\t\t┌" + "─" * (7 * n + 1) + "┐") # 317x317 - 999x999
- elif n >= 101:
- print("\t\t\t┌" + "─" * (6 * n + 1) + "┐") # 101x101 - 315x315
- elif n >= 33:
- print("\t\t\t┌" + "─" * (5 * n + 1) + "┐") # 33x33 - 99x99
- elif n >= 11:
- print("\t\t\t┌" + "─" * (4 * n + 1) + "┐") # 11x11 - 31x31
- else:
- print("\t\t\t┌" + "─" * (3 * n + 1) + "┐") # 3x3 - 9x9
- for i in range(n):
- print("\t\t\t│ ", end="")
- for j in range(n):
- # Format each number to have leading zeros if needed (up to 7-Digits)
- if n >= 1001:
- num_str = str(magicSquare[i][j]).zfill(7) # 7-Digit Values
- elif n >= 315:
- num_str = str(magicSquare[i][j]).zfill(6) # 6-Digit Values
- elif n >= 101:
- num_str = str(magicSquare[i][j]).zfill(5) # 5-Digit Values
- elif n >= 33:
- num_str = str(magicSquare[i][j]).zfill(4) # 4-Digit Values
- elif n >= 11:
- num_str = str(magicSquare[i][j]).zfill(3) # 3-Digit Values
- else:
- num_str = str(magicSquare[i][j]).zfill(2) # 2-Digit Values
- print(f"{num_str} ", end="")
- print("│") # Print vertical line
- # Print bottom border
- if n >= 1001:
- print("\t\t\t┌" + "─" * (8 * n + 1) + "┐") # 1001x1001 - Infinity (not padded further)
- elif n >= 319:
- print("\t\t\t└" + "─" * (7 * n + 1) + "┘") # 317x317 - 999x999
- elif n >= 101:
- print("\t\t\t└" + "─" * (6 * n + 1) + "┘") # 101x101 - 315x315
- elif n >= 33:
- print("\t\t\t└" + "─" * (5 * n + 1) + "┘") # 33x33 - 99x99
- elif n >= 11:
- print("\t\t\t└" + "─" * (4 * n + 1) + "┘") # 11x11 - 31x31
- else:
- print("\t\t\t└" + "─" * (3 * n + 1) + "┘") # 3x3 - 9x9
- # Print sum information
- print(
- "\n\t The sum of each row/column/diagonal is: " + str(n * (n**2 + 1) / 2),
- "\n",
- )
- # Main Function for Magic Square
- if __name__ == "__main__":
- """
- Main function to execute the script.
- """
- print("\t\t :: MAGIC SQUARE GENERATOR ::\n")
- n = int(
- input(
- "Enter an odd number greater than 1 as the size of the Magic Square (n x n): "
- )
- )
- if n <= 1:
- print(
- "\n\t- Magic squares for dimensions less than or equal to 1 are not allowed.\n"
- )
- else:
- magic_square(n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement