Modular Evaluation
Prev | Chapter 5. Основы GEL | Next

## Modular Evaluation

Genius implements modular arithmetic. To use it you just add "mod <integer>" after the expression. Example: `2^(5!) * 3^(6!) mod 5` It could be possible to do modular arithmetic by computing with integers and then modding in the end with the `%` operator, which simply gives the remainder, but that may be time consuming if not impossible when working with larger numbers. For example, `10^(10^10) % 6` will simply not work (the exponent will be too large), while `10^(10^10) mod 6` is instantaneous. The first expression first tries to compute the integer `10^(10^10)` and then find remainder after division by 6, while the second expression evaluates everything modulo 6 to begin with.

The inverses of numbers mod some integer are computed by writing them as rational numbers (as long as the desired inverse exists, of course). Examples:

    10^-1 mod 101
    1/10 mod 101

Modular evaluation also works with matrices including taking inverses, powers, and dividing. Example:

    A = [1,2;3,4]
    B = A^-1 mod 5
    A*B mod 5

This should yield the identity matrix as B will be the inverse of A mod 5.

Some functions such as `sqrt` or `log` work in a different way when in modulo mode. These will then work like their discrete versions working within the ring of integers you selected. For example:

    genius> sqrt(4) mod 7
    =
    [2, 5]
    genius> 2*2 mod 7
    = 4

`sqrt` will actually return all the possible square roots.

Do not chain mod operators, simply place it at the end of the computation, all computations in the expression on the left will be carried out in mod arithmetic. If you place a mod inside a mod, you will get unexpected results. If you simply want to mod a single number and control exactly when remainders are taken, best to use the `%` operator. When you need to chain several expressions in modular arithmetic with different divisors, it may be best to just split up the expression into several and use temporary variables to avoid a mod inside a mod.

Prev | Up | Next
Comments | Home | Список операторов GEL