10000 Added the magic square algorithm by bhuvanakundumani · Pull Request #59 · AllAlgorithms/python · GitHub
[go: up one dir, main page]

Skip to content

Added the magic square algorithm #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions math/magic_square.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@



"""A magic square is an N×N grid of numbers in which the entries in each row, column and main diagonal sum to the same number (equal to N(N2+1)/2)"""


import numpy as np


print("Hi! Welcome to the magic square algorithm")
print("Please enter the number for which you would want a magic sqaure to be printed. Please enter an odd number")

# The odd number for which the magic square is created is stored in the variable N

N = int(input())

# create a matrix with values 0 using numpy. The datatype is int for the elements in the matrix
magic_square = np.zeros((N,N), dtype=int)

n = 1
i, j = 0, N//2

# n iterates from 1 to N**2. The loop exits when n is equal to N**2

while n <= N**2:
# Start in the middle of the first row.
# (i = 0 and j = N//2 ) and the element at magic_square[i,j] is the middle in the first row.
# insert n = 1 to begin with at magic_square[i,j]
magic_square[i, j] = n
# increment n by 1
n += 1
# Move diagonally up and right, wrapping to the first column or last row if the move leads outside the grid

new_i, new_j = (i-1) % N, (j+1)% N

# if the cell is already filled with a number, move vertically down one space.
if magic_square[new_i, new_j]:
i += 1
else:
i, j = new_i, new_j

print(magic_square)
26 changes: 26 additions & 0 deletions math/pascals_triangle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

"""
Pascal's triangle is a triangular array of numbers in which those at the ends of the rows are 1 and each of the others is the sum of the nearest two numbers in the row above (the apex, 1, being at the top).

"""
print("Welcome to Pascal's traingle:")


n=int(input("Enter number of rows for the pascal's traingle: "))
a=[]
for i in range(n):
a.append([])
a[i].append(1)
for j in range(1,i):
a[i].append(a[i-1][j-1]+a[i-1][j])
if(n!=0):
a[i].append(1)


# printing pascal's triangle
print( " Your Pascal's traiange for the number {}".format(n))
for i in range(n):
print(" "*(n-i),end=" ",sep=" ")
for j in range(0,i+1):
print('{0:6}'.format(a[i][j]),end=" ",sep=" ")
print()
0