Jump to content

P-Adic square roots: Difference between revisions

max. p-adic precision with standard data types
(→‎{{header|Wren}}: Updated in line with changes to FB example.)
(max. p-adic precision with standard data types)
Line 47:
 
 
const emx = 6448
'exponent maximum
 
const amx = 6000700000
'tentative argument maximum
 
'------------------------------------------------
const Mxd = cdbl(2)^53 - 1
'max. float64 integer
 
const Pmax = 32749
'max. prime < 2^15
Line 93 ⟶ 96:
function padic.sqrt (byref g as ratio, byval sw as integer) as integer
dim as longint a = g.a, b = g.b
dim as longint f, q, x, pk
dim as long f1, r, s, t
dim i as integer, f as double
sqrt = 0
 
Line 152 ⟶ 155:
'find root for small p
for r = 1 to p1
fq = b * r * r - a
if fq mod p = 0 then exit for
next r
 
Line 181 ⟶ 184:
'evaluate f(x)
#macro evalf(x)
f = b * x * cdbl(x -/ apk)
qf -= fcdbl(a \/ pk)
'overflow
if f -> q * pkMxd then exit for
q = clngint(f)
#endmacro
 
Line 229 ⟶ 233:
function padic.crat (byval sw as integer) as ratio
dim as integer i, j, t = min(v, 0)
dim r as ratiolongint s, f aspk, doublepm
dim as long q, x, y, pk, p1
dim as longintdouble qf, sh
dim r as ratio
 
'weighted digit sum
s = 0: pk = 1
for i = t to k - 1 + v
p1pm = pk: pk *= p
 
if pk \ p1pm - p then
'overflow
pk = p1pm: exit for
end if
 
s += d(i) * p1pm '(mod pk)
next i
 
Line 249 ⟶ 254:
dim as longint m(1) = {pk, s}
dim as longint n(1) = {0, 1}
s = s * s + 1 ' norm(v)^2
h = cdbl(s) * s + 1
i = 0: j = 1
s = s * s + 1 ' norm(v)^2
 
'Lagrange's algorithm
do
f = m(i) * cdbl(m(j) / sh)
f += n(i) * cdbl(n(j) / sh)
 
'Euclidean step
Line 262 ⟶ 268:
n(i) -= q * n(j)
 
qf = sh
sh = mcdbl(i) * m(i) + n(i) * nm(i)
h += cdbl(n(i)) * n(i)
'compare norms
if sh < qf then
'interchange vectors
swap i, j
Line 375 ⟶ 382:
data 10496,497, 2,19
 
data 3141-577215,5926664901, 3,1523
data 271815403,28126685, 3,1318
 
data -1,1, 5,8
Line 383 ⟶ 390:
 
data 2,1, 7,8
data -264511696,28518621467, 7,911
data 3029-27764,482111521, 7,911
data 379-27584,44912953, 7,811
 
data 717-166420,8135131, 11,711
data 141414142,213135623, 41,5,15
data -255,256, 257,3
 
Line 423 ⟶ 430:
loop
 
end
system
</lang>
 
{{out|Examples}}
<pre>
Line 478 ⟶ 486:
 
 
3141-577215/5926664901 + O(3^1523)
lift: 340696644765673211 mod 3^1523
sqrt +/-
... 21 0 2 1 01 1 2 02 1 0 1 1 1 2 1 1 0 21 0 2 0 1 0
... 01 2 0 1 21 1 0 20 1 2 1 1 1 0 1 1 2 01 2 0 2 2 0
sqrt^2
20 1 2 1 1 20 0 2 1 1 0 0 1 1 0 2 0 1 1 0 1 0 0
-577215/664901
3141/5926
 
 
271815403/28126685 + O(3^1318)
lift: 693355238961782 mod 3^1318
sqrt +/-
... 01 2 1 1 2 02 1 2 02 01 01 1 2 2 1 1 0. 1
... 21 0 1 1 0 20 1 0 20 21 21 1 0 0 1 1 2. 02
sqrt^2
1 2 1 2 0 0 1 2 2 0 1 1 0 1 2 2 12. 0 01
15403/26685
2718/281
 
 
Line 538 ⟶ 546:
 
 
-2645 11696/28518621467 + O(7^911)
lift: 39082527967215488 mod 7^911
sqrt +/-
... 3 2 6 5 3 1 2 4 1 4. 1
... 3 4 0 1 3 5 4 2 5 2. 6
sqrt^2
3 3 1 1 3 3 4 4 5. 1 1
11696/621467
-2645/28518
 
 
3029-27764/482111521 + O(7^911)
lift: 31104424453209984 mod 7^911
sqrt +/-
... 51 24 51 4 2 41 3 5 36 12 13
... 15 42 15 2 4 25 3 1 30 54 64
sqrt^2
6 5 61 40 12 5 5 5 3 06 26 12
-27764/11521
3029/4821
 
 
379-27584/44912953 + O(7^811)
lift: 5550871239987302 mod 7^811
sqrt +/-
... 4 2 5 0 4 5 0 1 2 2 1
... 2 4 1 6 2 1 6 5 4 4 6
sqrt^2
3 2 6 5 3 1 2 4 1 4 1
-27584/12953
379/449
 
 
717-166420/8135131 + O(11^711)
lift: 528009334219617965 mod 11^711
sqrt +/-
... 21 103 85 7 0 10 9 10 5 0 5
... 89 07 25 3 10 910 1 0 5 10 6
sqrt^2
1 4 1 4 2 1 3 5 6 2 3
-166420/135131
717/8
 
 
141414142/213135623 + O(41^5^15)
lift: 25564902236397452 mod 41^5^15
sqrt +/-
... 90 0 0 4 4 1 380 0 4 2 0 4 3 60 82
... 314 394 4 0 0 3 4 4 0 2 344 0 1 4 333
sqrt^2
124 362 311 153 233 4 3 4 3 4 2 3 2 0 4
14142/135623
1414/213
 
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.