2title = "3a: Modular and Signed Ints"
5aliases = ["/docs/reference/library/3a"]
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
375represented with a two prepending `-`s, and negative integers are represented
376with two; `--1` and `-1`, respectively.
378Positive signed integers correspond to odd atoms of twice their absolute value,
379and negative signed integers correspond to atoms of twice their absolute value
380minus one. For example:
399Produces the absolute value of signed integer `a`.
403`a` is a signed integer.
412 ++ abs |=(a=@s (add (end 0 1 a) (rsh 0 1 a)))
430Produces the difference of `a` minus `b`.
434`a` is a signed integer.
436`b` is a signed integer.
445 ++ dif |= [a=@s b=@s]
446 (sum a (new !(syn b) (abs b)))
464Produces the remainder of `b` modulo `a`.
468`a` is a signed integer.
480 =+(c=(old a) ?:(-.c (mod +.c b) (sub b +.c)))
486 > `@s`(dul:si -1 --5)
494 > `@s`(dul:si -11 -61)
503Produces the quotient of `b` divided by `c`.
507`a` is a signed integer.
509`b` is a signed integer.
518 ++ fra |= [a=@s b=@s]
519 (new =(0 (mix (syn a) (syn b))) (div (abs a) (abs b)))
540Produces a signed integer from an atom `b`. The product's sign is determined
541by the value of flag `a`: `&` will result in a prepending `--`, and `|` will
542result in a prepending `-`.
558 `@s`?:(a (mul 2 b) ?:(=(0 b) 0 +((mul 2 (dec b)))))
580Sign and absolute value
582Produces a cell composed of a `%.y` or `%.n`, depending on whether `a` is
583positive or negative, and the absolute value of `a`.
587`a` is a signed integer.
591A cell composed of a `term` and an atom.
596 ++ old |=(a=@s [(syn a) (abs a)])
614Produces a signed integer by multiplying `a` and `b`.
618`a` is an unsigned integer.
620`b` is an unsigned integer.
625 ++ pro |= [a=@s b=@s]
626 (new =(0 (mix (syn a) (syn b))) (mul (abs a) (abs b)))
644Produces a signed integer that is the remainder of `a` divided by `b`.
648`a` is a signed integer.
650`b` is a signed integer.
659 ++ rem |=([a=@s b=@s] (dif a (pro b (fra a b))))
684Produces an atom by adding `a` and `b`.
688`a` is a signed integer.
690`b` is a signed integer.
699 ++ sum |= [a=@s b=@s]
700 =+ [c=(old a) d=(old b)]
703 (new & (add +.c +.d))
705 (new & (sub +.c +.d))
706 (new | (sub +.d +.c))
709 (new | (sub +.c +.d))
710 (new & (sub +.d +.c))
711 (new | (add +.c +.d))
729Multiplies the unsigned integer `a` by two, producing an atom.
733`a` is an unsigned integer.
742 ++ sun |=(a=@u (mul 2 a))
772Tests whether signed atom `a` is positive or negative. `%.y` is produced if `a`
773is positive, and `%.n` is produced if `a` is negative.
777`a` is a signed integer.
786 ++ syn |=(a=@s =(0 (end 0 1 a)))
804Compares `a` and `b` to see which is greater. If `a` is greater than `b`, `--1`
805is produced. If `b` is greater than `a`, `-1` is produced. If `a` and `b` are
806equal, `--0` is produced.
810`a` is a signed integer.
812`b` is a signed integer.
821 ++ 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