3 title/'3a: Modular and Signed Ints'
8# 3a: modular and signed ints
12Extended Euclidean algorithm
15[greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor)
16of `a` and `b`. Also produces `u` and `v` such that `au + bv = GCD(a, b)`.
26`d`, the greatest common divisor, is an atom.
28`u`, the coefficient of `a`, is a signed integer.
30`v`, the coefficient of `b`, is a signed integer.
38 =+ [c=(sun a) d=(sun b)]
39 =+ [u=[c=(sun 1) d=--0] v=[c=--0 d=(sun 1)]]
47 u [(dif d.u (pro q c.u)) c.u]
48 v [(dif d.v (pro q c.v)) c.v]
64Container 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)
114Produces the power of `b` raised to the `c`, with `a` as the modular base.
135 =+ d=$(b (rsh 0 1 b))
137 ?:(=(0 (end 0 1 b)) e (pro c e))
149 > (~(exp fo 195) 8 2)
152 > (~(exp fo 995) 8 2)
161Produces the quotient of `b` divided by `c`, with `a` as the modular base.
210Produces an atom by taking the signed modulus of `a` with the coefficient `u`;
211`u` is produced by taking the `++egcd` of `a` and `b`.
228 =+ c=(dul:si u:(egcd b a) a)
256Produces the multiplication of `b` and `c` modulo `a`.
293Produces the remainder of `b` modulo `a`.
325Produces the remainder of `(b + c) mod a`.
363Container core for signed integer functions.
375The signed-integer type is represented by the `@s` aura. Positive integers are
376represented with a two prepending `-`s, and negative integers are represented
377with two; `--1` and `-1`, respectively.
379Positive signed integers correspond to odd atoms of twice their absolute value,
380and negative signed integers correspond to atoms of twice their absolute value
381minus one. For example:
400Produces the absolute value of signed integer `a`.
404`a` is a signed integer.
413 ++ abs |=(a=@s (add (end 0 1 a) (rsh 0 1 a)))
431Produces the difference of `a` minus `b`.
435`a` is a signed integer.
437`b` is a signed integer.
446 ++ dif |= [a=@s b=@s]
447 (sum a (new !(syn b) (abs b)))
465Produces the remainder of `b` modulo `a`.
469`a` is a signed integer.
481 =+(c=(old a) ?:(-.c (mod +.c b) (sub b +.c)))
487 > `@s`(dul:si -1 --5)
495 > `@s`(dul:si -11 -61)
504Produces the quotient of `b` divided by `c`.
508`a` is a signed integer.
510`b` is a signed integer.
519 ++ fra |= [a=@s b=@s]
520 (new =(0 (mix (syn a) (syn b))) (div (abs a) (abs b)))
541Produces a signed integer from an atom `b`. The product's sign is determined
542by the value of flag `a`: `&` will result in a prepending `--`, and `|` will
543result in a prepending `-`.
559 `@s`?:(a (mul 2 b) ?:(=(0 b) 0 +((mul 2 (dec b)))))
581Sign and absolute value
583Produces a cell composed of a `%.y` or `%.n`, depending on whether `a` is
584positive or negative, and the absolute value of `a`.
588`a` is a signed integer.
592A cell composed of a `term` and an atom.
597 ++ old |=(a=@s [(syn a) (abs a)])
615Produces a signed integer by multiplying `a` and `b`.
619`a` is an unsigned integer.
621`b` is an unsigned integer.
626 ++ pro |= [a=@s b=@s]
627 (new =(0 (mix (syn a) (syn b))) (mul (abs a) (abs b)))
645Produces a signed integer that is the remainder of `a` divided by `b`.
649`a` is a signed integer.
651`b` is a signed integer.
660 ++ rem |=([a=@s b=@s] (dif a (pro b (fra a b))))
685Produces an atom by adding `a` and `b`.
689`a` is a signed integer.
691`b` is a signed integer.
700 ++ sum |= [a=@s b=@s]
701 =+ [c=(old a) d=(old b)]
704 (new & (add +.c +.d))
706 (new & (sub +.c +.d))
707 (new | (sub +.d +.c))
710 (new | (sub +.c +.d))
711 (new & (sub +.d +.c))
712 (new | (add +.c +.d))
730Multiplies the unsigned integer `a` by two, producing an atom.
734`a` is an unsigned integer.
743 ++ sun |=(a=@u (mul 2 a))
773Tests whether signed atom `a` is positive or negative. `%.y` is produced if `a`
774is positive, and `%.n` is produced if `a` is negative.
778`a` is a signed integer.
787 ++ syn |=(a=@s =(0 (end 0 1 a)))
805Compares `a` and `b` to see which is greater. If `a` is greater than `b`, `--1`
806is produced. If `b` is greater than `a`, `-1` is produced. If `a` and `b` are
807equal, `--0` is produced.
811`a` is a signed integer.
813`b` is a signed integer.
822 ++ 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