Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c1deadc

Browse filesBrowse files
authored
Merge pull request AllAlgorithms#59 from bhuvanakundumani/master
Added the magic square algorithm
2 parents adb00e9 + 5806a45 commit c1deadc
Copy full SHA for c1deadc

File tree

Expand file treeCollapse file tree

2 files changed

+68
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+68
-0
lines changed

‎math/magic_square.py

Copy file name to clipboard
+42Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
3+
4+
"""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)"""
5+
6+
7+
import numpy as np
8+
9+
10+
print("Hi! Welcome to the magic square algorithm")
11+
print("Please enter the number for which you would want a magic sqaure to be printed. Please enter an odd number")
12+
13+
# The odd number for which the magic square is created is stored in the variable N
14+
15+
N = int(input())
16+
17+
# create a matrix with values 0 using numpy. The datatype is int for the elements in the matrix
18+
magic_square = np.zeros((N,N), dtype=int)
19+
20+
n = 1
21+
i, j = 0, N//2
22+
23+
# n iterates from 1 to N**2. The loop exits when n is equal to N**2
24+
25+
while n <= N**2:
26+
# Start in the middle of the first row.
27+
# (i = 0 and j = N//2 ) and the element at magic_square[i,j] is the middle in the first row.
28+
# insert n = 1 to begin with at magic_square[i,j]
29+
magic_square[i, j] = n
30+
# increment n by 1
31+
n += 1
32+
# Move diagonally up and right, wrapping to the first column or last row if the move leads outside the grid
33+
34+
new_i, new_j = (i-1) % N, (j+1)% N
35+
36+
# if the cell is already filled with a number, move vertically down one space.
37+
if magic_square[new_i, new_j]:
38+
i += 1
39+
else:
40+
i, j = new_i, new_j
41+
42+
print(magic_square)

‎math/pascals_triangle.py

Copy file name to clipboard
+26Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
"""
3+
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).
4+
5+
"""
6+
print("Welcome to Pascal's traingle:")
7+
8+
9+
n=int(input("Enter number of rows for the pascal's traingle: "))
10+
a=[]
11+
for i in range(n):
12+
a.append([])
13+
a[i].append(1)
14+
for j in range(1,i):
15+
a[i].append(a[i-1][j-1]+a[i-1][j])
16+
if(n!=0):
17+
a[i].append(1)
18+
19+
20+
# printing pascal's triangle
21+
print( " Your Pascal's traiange for the number {}".format(n))
22+
for i in range(n):
23+
print(" "*(n-i),end=" ",sep=" ")
24+
for j in range(0,i+1):
25+
print('{0:6}'.format(a[i][j]),end=" ",sep=" ")
26+
print()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.