2g: Unsigned Powers
++pow
Computes a
raised to the power of b
, producing an atom.
Accepts
a
is an atom.
b
is an atom.
Produces
An atom.
Source
++ pow ~/ %pow |= [a=@ b=@] ?: =(b 0) 1 |- ?: =(b 1) a =+ c=$(b (div b 2)) =+ d=(mul c c) ?~ (dis b 1) d (mul d a)
Examples
> (pow 2 6)
64
> (pow 6 2)
36
> (pow 7 (add 2 2))
2.401
> (pow 2 0)
1
> `@ux`(pow 0x1b 2)
0x2d9
++sqt
Computes the square root of a
and its remainder.
Accepts
a
is an atom.
Produces
A cell of atoms.
Source
++ sqt ~/ %sqt |= a=@ ^- [p=@ q=@] ?~ a [0 0] =+ [q=(div (dec (xeb a)) 2) r=0] =- [-.b (sub a +.b)] ^= b |- =+ s=(add r (bex q)) =+ t=(mul s s) ?: =(q 0) ?:((lte t a) [s t] [r (mul r r)]) ?: (lte t a) $(r s, q (dec q)) $(q (dec q))
Examples
> (sqt 4)
[p=2 q=0]
> (sqt 6)
[p=2 q=2]
> (sqt 2)
[p=1 q=1]
> (sqt 0b1101)
[p=3 q=4]
> `@ux`(sqt 0b1101)
! exit