-
Notifications
You must be signed in to change notification settings - Fork 0
/
Recamans_Sequence_As_Semicircles.py
72 lines (56 loc) · 2.27 KB
/
Recamans_Sequence_As_Semicircles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Plot Recamán's sequence as semicircles (Python)
@ JanOnBread (https://github.com/JanOnBread)
"""
# Importing modules
import numpy as np
import matplotlib.pyplot as plt
def plot_squence_as_semicircle(k):
"""
Plots the sequence with term x_k, k being our input, as semicircles
jumping from each term.
"""
# Forming our sequence
x=[0]
for i in range (1, k + 1):
if x[i-1] - i > 0 and x[i-1] - i not in x:
x.append( x[i-1] - i)
else:
x.append ( x[i-1] + i)
def draw_semicircle (a,b):
"""
Draws a semicircle with the centre (|a+b|/2, 0) where a and b are the
start and end x values of the semi-circle.
"""
ax = plt.subplot(111)
#Finds our radius (r), range between our a and b (p), centre (h)
r = np.abs(a - b) / 2
p = np.linspace(a, b, 10000)
h = (b + a) /2
# Plugs r, p, h into our rearrange circle formula to find our y
y = np.sqrt(r**2 - (p - h)**2)
# Plots semicircles
# Drawn over or under semi circles depending on the iteration (i)
plt.rcParams["figure.figsize"] = (18,10)
if i % 2 != 0 :
ax.plot(p, -y, color = "black", linewidth=1)
else:
ax.plot(p, y, color= "black", linewidth=1)
ax.plot(a, 0, "o", color= "blue", markersize=3)
ax.plot(b, 0, "o", color= "blue", markersize=3)
# If this is our last semicircle, we plot the axis.
if i == k -1:
# Labling axix and font siz
plt.rc('font', size = 18)
plt.gca().set_aspect('equal', adjustable = 'box')
plt.xlabel("$x_n$", fontsize = 28)
plt.title("Sequence up n = {}". format (k), fontsize = 20)
# Removing grids
ax.tick_params(labelleft = False)
ax.tick_params(left = False)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
# Loop to plot each of out semi cicles
for i in range (0,k):
draw_semicircle(x[i], x[i+1])