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]
61Container core for modular arithmetic functions.
76Produces the difference between atoms `b` and `c`, with `a` as the modular base.
95 (sit (sub (add a b) (sit c)))
104 > (~(dif fo 21) 11 45)
113Produces the power of `c` raised to the `b`, with `a` as the modular base.
134 =+ d=$(b (rsh 0 1 b))
136 ?:(=(0 (end 0 1 b)) e (pro c e))
148 > (~(exp fo 195) 8 2)
151 > (~(exp fo 995) 8 2)
160Produces the quotient of `b` divided by `c`, with `a` as the modular base.
209Produces an atom by taking the signed modulus of `a` with the coefficient `u`;
210`u` is produced by taking the `++egcd` of `a` and `b`.
227 =+ c=(dul:si u:(egcd b a) a)
255Produces the multiplication of `b` and `c` modulo `a`.
292Produces the remainder of `b` modulo `a`.
324Produces the remainder of `(b + c) mod a`.
362Container core for signed integer functions.
374The signed-integer type is represented by the `@s` aura. Positive integers are
375prepended with a `--`, and negative integers are prepended with a `-`. For
376example, `--1` represents positive one, and `-1` represents negative one.
378Zig zag encoding is used to convert atoms to signed integers. Positive signed
379integers correspond to even atoms of twice their absolute value, and negative
380signed integers correspond to odd atoms of twice their absolute value minus
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