a guest
Mar 9th, 2021
51
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import math
2. import matplotlib.pyplot as plt
3.
4. def p_valuation(p, n):
5.
6.     if n == 0:
7.         return math.inf
8.
9.     result = 0
10.
11.     while n % p == 0:   # Note that "%" is the modulo operation
12.         n = n / p
13.         result += 1
14.
15.     return result
16.
17.
18. def p_norm(p, n):
19.     return p ** (- p_valuation(p, n))   # Note that "**" is exponentiation
20.
21.
22. index = 1
23.
24. def plot_convergence(ax, sequence, limit, p, name):
25.
26.     global index
27.
28.     distances = [p_norm(p, element - limit) for element in sequence]
29.
30.     ax.plot(range(len(sequence)), distances)
31.     ax.set_title(str(index))
32.     index += 1
33.
34.
35.
36. N = 20
37. P = 2
38.
39. fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, constrained_layout=True )
40.
41. plot_convergence(ax1, [P ** n for n in range(N)], 0, P, "p_power")
42. plot_convergence(ax2, [(P + 1) ** n for n in range(N)], 1, P, "non_p_power")
43. plot_convergence(ax3, [sum([P ** i for i in range(n)]) for n in range(N)], -1, P, "power_series")
44. plot_convergence(ax4, [math.factorial(n) for n in range(N)], 0, P, "factorial")
45.
46. plt.savefig("plots.png")
RAW Paste Data