diff --git a/Patterns/pascals_triangle.py b/Patterns/pascals_triangle.py new file mode 100644 index 0000000..f628758 --- /dev/null +++ b/Patterns/pascals_triangle.py @@ -0,0 +1,57 @@ +from math import factorial +from itertools import combinations + +#This is a recursive implementation for generating Pascal's triangle. +def recursive(n): + def recursive_row(n,k): + if k == 0 or k == n: + return 1 + return recursive_row(n-1, k-1) + recursive_row(n-1, k); + triangle = []; + for row in range(n): + curr_row = []; + for k in range(row+1): + curr_row.append(recursive_row(row, k)) + triangle.append(curr_row) + + return triangle + +#This is the typical definition of Pascal's triangle. +def bruteForce_sumLast(n): + triangle = [] + for row in range(n): + curr_row = [] + for i in range(row+1): + if i == 0 or i == row: + curr_row.append(1); + else: + curr_row.append(triangle[-1][i-1]+triangle[-1][i]) + triangle.append(curr_row) + return triangle + +#We define the n choose k function: https://en.wikipedia.org/wiki/Combination +#This will be useful in the next function. +def nchoosek(n,k): + #There are multiple ways of computing this aswell! + #For example, it can be computed recursively or via the definition nCk = n!/(k!*(n-k)!) + return len(list(combinations(range(n),k))) + +#This is the definition of the Pascal's triangle using combinatorial numbers. +def bruteForce_factorial(n): + triangle = [] + for row in range(n): + curr_row = [] + for col in range(row+1): + curr_row.append( nchoosek(row, col) ) + triangle.append(curr_row) + return triangle + + +#This algorithm is taken from user MortalViews in StackExchange from the post +#https://stackoverflow.com/questions/24093387/pascals-triangle-for-python +#This is just for visualization purposes! +#Just enter the output of any of the above functions and it will work. +def draw_beautiful(ps): + max = len(' '.join(map(str,ps[-1]))) + for p in ps: + print(' '.join(map(str,p)).center(max)+'\n') diff --git a/Patterns/sierpinskys_triangle.py b/Patterns/sierpinskys_triangle.py new file mode 100644 index 0000000..4b98531 --- /dev/null +++ b/Patterns/sierpinskys_triangle.py @@ -0,0 +1,15 @@ +import matplotlib.pylab as plt +from random import randint + +def midpoint(p1, p2): + return ((p1[0]+p2[0])/2, (p1[1]+p2[1])/2) + +width, height = 1, 1 +p, q, r = (width/2, height), (0, 0), (width, 0); #This three points form a triangle +triangle = [p,q,r] +mid_point = (width/2, height/2) + +for _ in range(5000): #if you increase this number, then the fractal will be more defined! + mid_point = midpoint(mid_point, triangle[randint(0,2)]) + plt.plot(mid_point[0], mid_point[1], 'm.', markersize=2) +plt.show() \ No newline at end of file