A small kernel of code for playing with Galois fields of arbitrary characteristic
修订版 | 430a429881135d4234982e160ff76cc009132f13 (tree) |
---|---|
时间 | 2021-03-15 14:52:04 |
作者 | Eric Hopper <hopper@omni...> |
Commiter | Eric Hopper |
Merge in ratpi to grabbag.
@@ -15,6 +15,7 @@ | ||
15 | 15 | bitpool >>= 1 |
16 | 16 | --bitpoolbits |
17 | 17 | |
18 | + | |
18 | 19 | def inewt(b, n): |
19 | 20 | newest = b >> (b.bit_length() - (b.bit_length() // n)) |
20 | 21 | est = 0 |
@@ -26,6 +27,7 @@ | ||
26 | 27 | return min(est, newest) |
27 | 28 | return newest |
28 | 29 | |
30 | + | |
29 | 31 | def factors(n): |
30 | 32 | factors = [] |
31 | 33 | while n & 1 == 0: |
@@ -43,8 +45,10 @@ | ||
43 | 45 | factors.append(n) |
44 | 46 | return factors |
45 | 47 | |
48 | + | |
46 | 49 | import math |
47 | 50 | |
51 | + | |
48 | 52 | def nth_root_big(b, n): |
49 | 53 | if b.bit_length() <= 1023: |
50 | 54 | return math.pow(b, 1/n) |
@@ -58,3 +62,13 @@ | ||
58 | 62 | while (len(flist) > 0) and (runprod < chop_factor): |
59 | 63 | runprod *= flist.pop() |
60 | 64 | return math.pow(inewt(b, runprod), 1 / (n // runprod)) |
65 | + | |
66 | + | |
67 | +from fractions import Fraction as F | |
68 | + | |
69 | + | |
70 | +def ratpi(n): | |
71 | + """I have no better place to put this. This is a quickly converging series | |
72 | + for computing a rational approximation of pi. For when you want more | |
73 | + precision than math.pi will give you.""" | |
74 | + return sum(F(1/16**k)*(F(4,8*k+1) - F(2,8*k + 4) - F(1,8*k+5) - F(1, 8*k+6)) for k in range(0, n)) |
@@ -1,8 +0,0 @@ | ||
1 | -from fractions import Fraction as F | |
2 | - | |
3 | -"""I have no better place to put this. This is a quickly converging series for | |
4 | -computing a rational approximation of pi. For when you want more precision | |
5 | -than math.pi will give you.""" | |
6 | - | |
7 | -def ratpi(n): | |
8 | - return sum(F(1/16**k)*(F(4,8*k+1) - F(2,8*k + 4) - F(1,8*k+5) - F(1, 8*k+6)) for k in range(0, n)) |