2title = "3a: Modular and Signed Ints"
6# 3a: modular and signed ints
10Extended Euclidean algorithm
13[greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor)
14of `a` and `b`. Also produces `u` and `v` such that `au + bv = GCD(a, b)`.
24`d`, the greatest common divisor, is an atom.
26`u`, the coefficient of `a`, is a signed integer.
28`v`, the coefficient of `b`, is a signed integer.
36 =+ [c=(sun a) d=(sun b)]
37 =+ [u=[c=(sun 1) d=--0] v=[c=--0 d=(sun 1)]]
45 u [(dif d.u (pro q c.u)) c.u]
46 v [(dif d.v (pro q c.v)) c.v]
62Container 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)
112Produces the power of `b` raised to the `c`, with `a` as the modular base.
133 =+ d=$(b (rsh 0 1 b))
135 ?:(=(0 (end 0 1 b)) e (pro c e))
147 > (~(exp fo 195) 8 2)
150 > (~(exp fo 995) 8 2)
159Produces the quotient of `b` divided by `c`, with `a` as the modular base.
208Produces an atom by taking the signed modulus of `a` with the coefficient `u`;
209`u` is produced by taking the `++egcd` of `a` and `b`.
226 =+ c=(dul:si u:(egcd b a) a)
254Produces the multiplication of `b` and `c` modulo `a`.
291Produces the remainder of `b` modulo `a`.
323Produces the remainder of `(b + c) mod a`.
361Container core for signed integer functions.
373The signed-integer type is represented by the `@s` aura. Positive integers are
374represented with a two prepending `-`s, and negative integers are represented
375with two; `--1` and `-1`, respectively.
377Positive signed integers correspond to odd atoms of twice their absolute value,
378and negative signed integers correspond to atoms of twice their absolute value
379minus one. For example:
398Produces the absolute value of signed integer `a`.
402`a` is a signed integer.
411 ++ abs |=(a=@s (add (end 0 1 a) (rsh 0 1 a)))
429Produces the difference of `a` minus `b`.
433`a` is a signed integer.
435`b` is a signed integer.
444 ++ dif |= [a=@s b=@s]
445 (sum a (new !(syn b) (abs b)))
463Produces the remainder of `b` modulo `a`.
467`a` is a signed integer.
479 =+(c=(old a) ?:(-.c (mod +.c b) (sub b +.c)))
485 > `@s`(dul:si -1 --5)
493 > `@s`(dul:si -11 -61)
502Produces the quotient of `b` divided by `c`.
506`a` is a signed integer.
508`b` is a signed integer.
517 ++ fra |= [a=@s b=@s]
518 (new =(0 (mix (syn a) (syn b))) (div (abs a) (abs b)))
539Produces a signed integer from an atom `b`. The product's sign is determined
540by the value of flag `a`: `&` will result in a prepending `--`, and `|` will
541result in a prepending `-`.
557 `@s`?:(a (mul 2 b) ?:(=(0 b) 0 +((mul 2 (dec b)))))
579Sign and absolute value
581Produces a cell composed of a `%.y` or `%.n`, depending on whether `a` is
582positive or negative, and the absolute value of `a`.
586`a` is a signed integer.
590A cell composed of a `term` and an atom.
595 ++ old |=(a=@s [(syn a) (abs a)])
613Produces a signed integer by multiplying `a` and `b`.
617`a` is an unsigned integer.
619`b` is an unsigned integer.
624 ++ pro |= [a=@s b=@s]
625 (new =(0 (mix (syn a) (syn b))) (mul (abs a) (abs b)))
643Produces a signed integer that is the remainder of `a` divided by `b`.
647`a` is a signed integer.
649`b` is a signed integer.
658 ++ rem |=([a=@s b=@s] (dif a (pro b (fra a b))))
683Produces an atom by adding `a` and `b`.
687`a` is a signed integer.
689`b` is a signed integer.
698 ++ sum |= [a=@s b=@s]
699 =+ [c=(old a) d=(old b)]
702 (new & (add +.c +.d))
704 (new & (sub +.c +.d))
705 (new | (sub +.d +.c))
708 (new | (sub +.c +.d))
709 (new & (sub +.d +.c))
710 (new | (add +.c +.d))
728Multiplies the unsigned integer `a` by two, producing an atom.
732`a` is an unsigned integer.
741 ++ sun |=(a=@u (mul 2 a))
771Tests whether signed atom `a` is positive or negative. `%.y` is produced if `a`
772is positive, and `%.n` is produced if `a` is negative.
776`a` is a signed integer.
785 ++ syn |=(a=@s =(0 (end 0 1 a)))
803Compares `a` and `b` to see which is greater. If `a` is greater than `b`, `--1`
804is produced. If `b` is greater than `a`, `-1` is produced. If `a` and `b` are
805equal, `--0` is produced.
809`a` is a signed integer.
811`b` is a signed integer.
820 ++ 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