on 4fdc19d help sign in
105 lines 1.2 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---
55## `++sqt`
57Computes the square root of `a` and its remainder.
59#### Accepts
61`a` is an atom.
63#### Produces
65A cell of atoms.
67#### Source
69```hoon
70++ sqt
71 ~/ %sqt
72 |= a=@ ^- [p=@ q=@]
73 ?~ a [0 0]
74 =+ [q=(div (dec (xeb a)) 2) r=0]
75 =- [-.b (sub a +.b)]
76 ^= b |-
77 =+ s=(add r (bex q))
78 =+ t=(mul s s)
79 ?: =(q 0)
80 ?:((lte t a) [s t] [r (mul r r)])
81 ?: (lte t a)
82 $(r s, q (dec q))
83 $(q (dec q))
84```
86#### Examples
88```
89 > (sqt 4)
90 [p=2 q=0]
92 > (sqt 6)
93 [p=2 q=2]
95 > (sqt 2)
96 [p=1 q=1]
98 > (sqt 0b1101)
99 [p=3 q=4]
101 > `@ux`(sqt 0b1101)
102 ! exit
103```
105---