on b00decd help sign in
105 lines 1.3 kB history raw
1+++
2title = "2g: Unsigned Powers"
3weight = 10
4template = "doc.html"
5+++
7### `++pow`
9Computes `a` raised to the power of `b`, producing an atom.
11#### Accepts
13`a` is an atom.
15`b` is an atom.
17#### Produces
19An atom.
21#### Source
23```hoon
24 ++ pow
25 ~/ %pow
26 |= [a=@ b=@]
27 ?: =(b 0) 1
28 |- ?: =(b 1) a
29 =+ c=$(b (div b 2))
30 =+ d=(mul c c)
31 ?~ (dis b 1) d (mul d a)
32```
34#### Examples
36```
37 > (pow 2 6)
38 64
40 > (pow 6 2)
41 36
43 > (pow 7 (add 2 2))
44 2.401
46 > (pow 2 0)
47 1
49 > `@ux`(pow 0x1b 2)
50 0x2d9
51```
53---
54### `++sqt`
56Computes the square root of `a` and its remainder.
58#### Accepts
60`a` is an atom.
62#### Produces
64A cell of atoms.
66#### Source
68```hoon
69 ++ sqt
70 ~/ %sqt
71 |= a=@ ^- [p=@ q=@]
72 ?~ a [0 0]
73 =+ [q=(div (dec (xeb a)) 2) r=0]
74 =- [-.b (sub a +.b)]
75 ^= b |-
76 =+ s=(add r (bex q))
77 =+ t=(mul s s)
78 ?: =(q 0)
79 ?:((lte t a) [s t] [r (mul r r)])
80 ?: (lte t a)
81 $(r s, q (dec q))
82 $(q (dec q))
83```
85#### Examples
87```
88 > (sqt 4)
89 [p=2 q=0]
91 > (sqt 6)
92 [p=2 q=2]
94 > (sqt 2)
95 [p=1 q=1]
97 > (sqt 0b1101)
98 [p=3 q=4]
100 > `@ux`(sqt 0b1101)
101 ! exit
102```
105---