Question

Write a Python program that generate randomly a magic square of 3 x 3 elements. For...

Write a Python program that generate randomly a magic square of 3 x 3 elements. For example:

4

3

8

9

5

1

2

7

6

    

= Matrix1

Matrix 1 above is an example of magic square. The rows total, the columns total, and the diagonal totals are all 15.

Your program should randomly generate a 3x3 matrix. Check if it is a magic square. If it is a magic square, then print some information and quit. If the generated square is not magic, then increment the counter of the tries, print it, and try again. Keep on trying until you find a magic square. Your program should keep on trying automatically until it finds a magic square.

I attached a Python program with informal description and algorithm of how the program should work. You may copy it and use it to write the program. If you want to start from scratch and built your own program, you can do that as long as you use a random function to create a 3x3 matrix, then test it if it is a magic square, and finally print it with required information whenever it is found.

Below is what your program should display on the screen when you run it.

1

2

3

:

:

:

82222

82223

82224

It is a magic square

[[6, 7, 2], [1, 5, 9], [8, 3, 4]]

It started at : 1584806306.7437787 using seconds

It ended   at : 1584806608.498532 using seconds

It started at : Sat Mar 21 11:58:26 2020

It ended   at : Sat Mar 21 12:03:28 2020

It took 301 seconds to find the magic square

Notice that we have a billion option to test a 9 digits’ number. In the example above, it took 82,224 tries to find that magic square. The time required to do these many random combination and test them is 301 seconds, which is equal to 5 minutes.

Your program may find the magic square in a different number of tries of course, since it is random.

Below is a copy of the attached Python magic square program that you may start with.

--------------------------------Beginning of the Python program-------------------------------

# Name

# Date

# Magic Squre Program

import random

import time

tim_st=time.time()

tim_st2=time.ctime()

s=0 # s is a counter that prints how many tries have been made

while True: # keep trying until you find a magic square

    magic=True # assume that magic is True, as a start

    mat1=[] # intialize matrix1 (mat1) to hold 9 unique numbers

    i=1

    while i < 10:

        # generate a random number between 1 and 9

        # check if the generated number exists in the list, if it exists try

        # again, if it doesn't exist in the list then append it to the list

    # intialize mat2 Multi-dimensional list with

    # 3 rows and 3 columns

    # Transfer the nine random number into the mat2 in order 3 x 3 x 3  

    ''' this is two possible magic squares

        matrix=[[2,7,6],[9,5,1],[4,3,8]]

        matrix=[[6,1,8],[7,5,3],[2,9,4]]

    '''   

    ##### Sum of the first row, call it sum1

    ##### Sum of rows, and check if each row's sum equal to sum1

    ##### Sum of columns, and check if each column's sum equal to sum1       

    ##### Sum of first Diagonal, and check if the sum equal to sum1

    ##### Sum of second Diagonal, and check if the sum equal to sum1

    # if it is a magic square:

    # 1- print the magic square

    # 2- print the time it took to find one randomly

    # 3- break out of the loop, STOP the program

    # increment the counter 's' of tries and print it

   

--------------------------------End of the Python program-------------------------------

There are many programs available in the WWW to generate magic square according to some known patterns. You are not asked to copy any of them.

You are asked to use a random function to generate a 3x3 matrix, then check if it is a magic square, and keep on trying until you get one.

Any other algorithm you use to write the program will not be accepted if you don’t use the requested random generating algorithm.

This assignment gives you a chance to exercise on simulation of a problem in your field of specialty according to some given factors where you may notice the result according to random sample set of data.

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
Write a Python program that generate randomly a magic square of 3 x 3 elements. For...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT