2title = "3a: Modular and Signed Ints"
9Extended Euclidean algorithm
12[greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor)
13of `a` and `b`. Also produces `u` and `v` such that `au + bv = GCD(a, b)`.
23`d`, the greatest common divisor, is an atom.
25`u`, the coefficient of `a`, is a signed integer.
27`v`, the coefficient of `b`, is a signed integer.
35 =+ [c=(sun a) d=(sun b)]
36 =+ [u=[c=(sun 1) d=--0] v=[c=--0 d=(sun 1)]]
44 u [(dif d.u (pro q c.u)) c.u]
45 v [(dif d.v (pro q c.v)) c.v]
62Container core for modular arithmetic functions.
78Produces the difference between atoms `b` and `c`, with `a` as the modular base.
97 (sit (sub (add a b) (sit c)))
106 > (~(dif fo 21) 11 45)
116Produces the power of `c` raised to the `b`, with `a` as the modular base.
137 =+ d=$(b (rsh 0 1 b))
139 ?:(=(0 (end 0 1 b)) e (pro c e))
151 > (~(exp fo 195) 8 2)
154 > (~(exp fo 995) 8 2)
164Produces the quotient of `b` divided by `c`, with `a` as the modular base.
214Produces an atom by taking the signed modulus of `a` with the coefficient `u`;
215`u` is produced by taking the `++egcd` of `a` and `b`.
232 =+ c=(dul:si u:(egcd b a) a)
261Produces the multiplication of `b` and `c` modulo `a`.
299Produces the remainder of `b` modulo `a`.
332Produces the remainder of `(b + c) mod a`.
371Container core for signed integer functions.
383The signed-integer type is represented by the `@s` aura. Positive integers are
384prepended with a `--`, and negative integers are prepended with a `-`. For
385example, `--1` represents positive one, and `-1` represents negative one.
387Zig zag encoding is used to convert atoms to signed integers. Positive signed
388integers correspond to even atoms of twice their absolute value, and negative
389signed integers correspond to odd atoms of twice their absolute value minus
410Produces the absolute value of signed integer `a`.
414`a` is a signed integer.
423 ++ abs |=(a=@s (add (end 0 1 a) (rsh 0 1 a)))
442Produces the difference of `a` minus `b`.
446`a` is a signed integer.
448`b` is a signed integer.
457 ++ dif |= [a=@s b=@s]
458 (sum a (new !(syn b) (abs b)))
477Produces the remainder of `b` modulo `a`.
481`a` is a signed integer.
493 =+(c=(old a) ?:(-.c (mod +.c b) (sub b +.c)))
499 > `@s`(dul:si -1 --5)
507 > `@s`(dul:si -11 -61)
517Produces the quotient of `b` divided by `c`.
521`a` is a signed integer.
523`b` is a signed integer.
532 ++ fra |= [a=@s b=@s]
533 (new =(0 (mix (syn a) (syn b))) (div (abs a) (abs b)))
555Produces a signed integer from an atom `b`. The product's sign is determined
556by the value of flag `a`: `&` will result in a prepending `--`, and `|` will
557result in a prepending `-`.
573 `@s`?:(a (mul 2 b) ?:(=(0 b) 0 +((mul 2 (dec b)))))
596Sign and absolute value
598Produces a cell composed of a `%.y` or `%.n`, depending on whether `a` is
599positive or negative, and the absolute value of `a`.
603`a` is a signed integer.
607A cell composed of a `term` and an atom.
612 ++ old |=(a=@s [(syn a) (abs a)])
631Produces a signed integer by multiplying `a` and `b`.
635`a` is an unsigned integer.
637`b` is an unsigned integer.
642 ++ pro |= [a=@s b=@s]
643 (new =(0 (mix (syn a) (syn b))) (mul (abs a) (abs b)))
662Produces a signed integer that is the remainder of `a` divided by `b`.
666`a` is a signed integer.
668`b` is a signed integer.
677 ++ rem |=([a=@s b=@s] (dif a (pro b (fra a b))))
702Produces an atom by adding `a` and `b`.
706`a` is a signed integer.
708`b` is a signed integer.
717 ++ sum |= [a=@s b=@s]
718 =+ [c=(old a) d=(old b)]
721 (new & (add +.c +.d))
723 (new & (sub +.c +.d))
724 (new | (sub +.d +.c))
727 (new | (sub +.c +.d))
728 (new & (sub +.d +.c))
729 (new | (add +.c +.d))
748Multiplies the unsigned integer `a` by two, producing an atom.
752`a` is an unsigned integer.
761 ++ sun |=(a=@u (mul 2 a))
792Tests whether signed atom `a` is positive or negative. `%.y` is produced if `a`
793is positive, and `%.n` is produced if `a` is negative.
797`a` is a signed integer.
806 ++ syn |=(a=@s =(0 (end 0 1 a)))
825Compares `a` and `b` to see which is greater. If `a` is greater than `b`, `--1`
826is produced. If `b` is greater than `a`, `-1` is produced. If `a` and `b` are
827equal, `--0` is produced.
831`a` is a signed integer.
833`b` is a signed integer.
842 ++ cmp |= [a=@s b=@s]
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list