p-adic numbers
Measure a number by how often p divides it, and the integers acquire a new completion: numbers with infinitely many digits to the left. Here a p-adic number is a rule that produces as many digits as you ask for, exactly.
Valuation and the p-adic size
For a rational , is the exponent of in and . So and : being divisible by makes a number small. This size satisfies the strong triangle inequality , and with the ordinary size it gives the product formula .
import { padic, padicValuation, padicNorm, expansion, henselRoot, teichmuller } from "@ratmath/numtheory";
padicValuation("50/27", 5n); // 2
String(padicNorm("50/27", 3n)); // "27"
padic("50/27", 3n).toString(); // "…00000000000000001.212"
Infinitely many digits to the left
Completing with gives , whose elements are with digits , written right to left. In , : add 1 and every digit carries. A rational's expansion is eventually periodic, and conversely.
padic(-1, 5n).toString(); // "…44444444444444444444"
padic("1/3", 5n).toString(); // "…31313131313131313132": 3 times it is …0001
expansion("1/3", 5n); // { valuation: 0, preperiod: [2n], period: [3n, 1n] }
padic("1/10", 5n).toString(); // "…2222222222222222222.3"
Lazy and exact
A Padic is a rule: given , it returns a rational with . Rationals are exact. A sum asks its parts for precision ; a product asks each for minus the other's valuation; a quotient first finds the divisor's valuation. Nothing is fixed in advance, so asking for more digits reruns the whole computation to that depth, and results are cached. What cannot be decided by digits is reported: the valuation of a number that may be zero is searched for, and the search gives up with the code "unknown-valuation" rather than claiming a zero.
Square roots and Hensel
For odd , a unit is a square in exactly when it is a square mod ; for , when . Then Newton's iteration from a root mod converges -adically, doubling the correct digits each step. henselRoot does the same for any polynomial, given a start with .
const i = padic(-1, 5n).sqrt();
i.toString(); // "…40423140223032431212": a square root of −1 in Z₅
i.mul(i).toString(); // "…44444444444444444444"
padic(-7, 2n).sqrt(); // exists: −7 ≡ 1 (mod 8)
padic(2, 5n).sqrt(); // throws "not-a-square": 2 is not a square mod 5
henselRoot([-2, 0, 0, 1], 3, 5n).toString(); // "…21134404003204132203", the cube root of 2 ≡ 3 (mod 5)
exp, log and roots of unity
The series converges when ( for ), since . The series converges for , and extends to every unit by ; on the domain of exp the two are inverse. The Teichmüller representative is the -th root of unity congruent to : in , is the square root of −1 above.
padic(5, 5n).exp().log().toString(); // "…000000000000000000010": back to 5
teichmuller(2n, 5n).toString(); // "…40423140223032431212"
teichmuller(2n, 5n).pow(4).toString(); // "…00000000000000000001"
@ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.