on c19bc7a help sign in
108 lines 1.3 kB history raw
1:- :~ navhome/'/docs/'
2 sort/'10'
3 title/'2g: Unsigned Powers'
4 ==
6;>
8# 2g: unsigned powers
10### `++pow`
12Computes `a` raised to the power of `b`, producing an atom.
14#### Accepts
16`a` is an atom.
18`b` is an atom.
20#### Produces
22An atom.
24#### Source
26```
27 ++ pow
28 ~/ %pow
29 |= [a=@ b=@]
30 ?: =(b 0) 1
31 |- ?: =(b 1) a
32 =+ c=$(b (div b 2))
33 =+ d=(mul c c)
34 ?~ (dis b 1) d (mul d a)
35```
37#### Examples
39```
40 > (pow 2 6)
41 64
43 > (pow 6 2)
44 36
46 > (pow 7 (add 2 2))
47 2.401
49 > (pow 2 0)
50 1
52 > `@ux`(pow 0x1b 2)
53 0x2d9
54```
56---
57### `++sqt`
59Computes the square root of `a` and its remainder.
61#### Accepts
63`a` is an atom.
65#### Produces
67A cell of atoms.
69#### Source
71```
72 ++ sqt
73 ~/ %sqt
74 |= a=@ ^- [p=@ q=@]
75 ?~ a [0 0]
76 =+ [q=(div (dec (xeb a)) 2) r=0]
77 =- [-.b (sub a +.b)]
78 ^= b |-
79 =+ s=(add r (bex q))
80 =+ t=(mul s s)
81 ?: =(q 0)
82 ?:((lte t a) [s t] [r (mul r r)])
83 ?: (lte t a)
84 $(r s, q (dec q))
85 $(q (dec q))
86```
88#### Examples
90```
91 > (sqt 4)
92 [p=2 q=0]
94 > (sqt 6)
95 [p=2 q=2]
97 > (sqt 2)
98 [p=1 q=1]
100 > (sqt 0b1101)
101 [p=3 q=4]
103 > `@ux`(sqt 0b1101)
104 ! exit
105```
108---