 # p-adic square root approximation

a guest
Mar 21st, 2021
37
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. import fractions
5.
6. def p_valuation(p, n):
7.
8.     if n == 0:
9.         return math.inf
10.
11.     result = 0
12.
13.     while n % p == 0:   # Note that "%" is the modulo operation
14.         n = n / p
15.         result += 1
16.
17.     return result
18.
19.
20. def p_norm(p, z):
21.     return p ** (p_valuation(p, z.denominator) - p_valuation(p, z.numerator))   # Note that "**" is exponentiation
22.
23.
24. def one_plus_square_approximate(x, N):
25.
26.     s = 1
27.
28.     for i in range(1, N + 1):
29.         numerator = (-1)**(i + 1) * math.factorial(2 * i) * (x ** i)
30.         denominator = (4 ** i) * (math.factorial(i) ** 2) * (2 * i - 1)
31.
32.         s += fractions.Fraction(numerator, denominator)
33.
34.     return s
35.
36.
37. for N in range(1, 10):
38.     approximation = int(one_plus_square_approximate(-8, N))
39.     square = approximation ** 2
40.     diff = square + 7
41.     diff_norm = p_norm(2, diff)
42.
43.     print("{:15d} &  {:15d} &  {:15d} &  {:15d} &  {:15f} \\\\".format(N, approximation, square, diff, diff_norm))
44.
RAW Paste Data