1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
use Display;
use crate::;
use ;
/// An adic number with only integer digits
///
/// # Adic Integer
///
/// Structs implementing this trait represent adic integers, representable as base-p digital expansions,
/// with a possibly-infinite number of digits to the left of a decimal point.
///
/// There is a distinction between adic NUMBERS and adic INTEGERS.
/// Adic integers are adic numbers without digits to the right of the decimal.
/// These are numbers without powers of p in their denominator, if viewed akin to a rational.
/// Using the p-adic norm, these are exactly the numbers where the valuation v >= 0, i.e. `|x| = p^(-v) <= 1`.
/// In the reals, all nonzero integers have a size greater than or equal to 1.
/// In the adics, it is the opposite; all integers have size less than or equal to 1.
///
/// ```
/// # use adic::{normed::Normed, UAdic};
/// # use num::rational::Ratio;
/// assert_eq!(Ratio::new(1, 1), UAdic::new(5, vec![4, 1, 3, 2]).norm());
/// assert_eq!(Ratio::new(1, 25), UAdic::new(5, vec![0, 0, 3, 2]).norm());
/// ```
///
/// Many of the same operations are possible with these integers: addition, multiplication, powers, roots.
/// Division is possible, but it can take you out of the integers just like in the reals.
///
/// Perhaps surprisingly, roots of adic integers are also adic integers (if they exist).
/// While `sqrt(2) = {1.414..., -1.414...}` in the reals, giving fractional digits,
/// in the 7-adics, `sqrt(2) = {...6213._7, ...0454._7}`.
///
/// This is because the rationals like (3/4) are representable in e.g. the 5-adics without nonzero digits to the right of the decimal.
/// Roots tend to fall "between" integers, which in the reals necessarily falls into the fraction space.
/// In the adics, the numbers "between" integers are just more integers!
///
/// ```
/// # use adic::{traits::AdicInteger, UAdic, Variety, ZAdic};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// assert_eq!(
/// "variety(...6213._7, ...0454._7)",
/// UAdic::new(7, vec![2]).nth_root(2, 4)?.to_string()
/// );
/// # Ok(()) }
/// ```
///
/// Another interesting observation is that there is no need for a negative sign.
/// In the reals, -1 is distinct and NEEDS a negative sign to be expressed.
/// In the 5-adics, `"-1" = ...44444._5`, since if you add 1 to that integer, you overflow and carry the 1 and overflow and carry...
///
/// ```
/// # use adic::{traits::AdicPrimitive, EAdic};
/// let one = EAdic::one(5);
/// let neg_one = -one.clone();
/// assert_eq!("1._5", one.to_string());
/// assert_eq!("(4)._5", neg_one.to_string());
/// assert_eq!("0._5", (one.clone() + neg_one.clone()).to_string());
/// assert_eq!("2._5", (one.clone() - neg_one.clone()).to_string());
/// assert_eq!("(4)3._5", (-one.clone() + neg_one.clone()).to_string());
/// ```
///
/// <https://en.wikipedia.org/wiki/P-adic_number#p-adic_integers>