0 Modulo Any Number

0 views
Skip to first unread message

Leanna Perr

unread,
Aug 3, 2024, 4:18:30 PM8/3/24
to alseotravun

In mathematics, the mod is also known as the modulo or the modulus. The modulo is defined as a remainder value when two numbers are divided. The mathematical representation of the modulo function is given as a mod b, where a and b are two numbers.

For example, the expression "5 mod 2" evaluates to 1, because 5 divided by 2 has a quotient of 2 and a remainder of 1, while "9 mod 3" would evaluate to 0, because 9 divided by 3 has a quotient of 3 and a remainder of 0.

In mathematics, the result of the modulo operation is an equivalence class, and any member of the class may be chosen as representative; however, the usual representative is the least positive residue, the smallest non-negative integer that belongs to that class (i.e., the remainder of the Euclidean division).[2] However, other conventions are possible. Computers and calculators have various ways of storing and representing numbers; thus their definition of the modulo operation depends on the programming language or the underlying hardware.

This still leaves a sign ambiguity if the remainder is non-zero: two possible choices for the remainder occur, one negative and the other positive; that choice determines which of the two consecutive quotients must be used to satisfy equation (1). In number theory, the positive remainder is always chosen, but in computing, programming languages choose depending on the language and the signs of a or n.[a] Standard Pascal and ALGOL 68, for example, give a positive remainder (or 0) even for negative divisors, and some programming languages, such as C90, leave it to the implementation when either of n or a is negative (see the table under In programming languages for details). a modulo 0 is undefined in most systems, although some do define it as a.

If both the dividend and divisor are positive, then the truncated, floored, and Euclidean definitions agree.If the dividend is positive and the divisor is negative, then the truncated and Euclidean definitions agree.If the dividend is negative and the divisor is positive, then the floored and Euclidean definitions agree.If both the dividend and divisor are negative, then the truncated and floored definitions agree.

Boute argues that Euclidean division is superior to the other ones in terms of regularity and useful mathematical properties, although floored division, promoted by Knuth, is also a good definition. Despite its widespread use, truncated division is shown to be inferior to the other definitions.

Modulo operations might be implemented such that a division with a remainder is calculated each time. For special cases, on some hardware, faster alternatives exist. For example, the modulo of powers of 2 can alternatively be expressed as a bitwise AND operation (assuming x is a positive integer, or using a non-truncating definition):

In addition, many computer systems provide a divmod functionality, which produces the quotient and the remainder at the same time. Examples include the x86 architecture's IDIV instruction, the C programming language's div() function, and Python's divmod() function.

Despite the mathematical elegance of Knuth's floored division and Euclidean division, it is generally much more common to find a truncated division-based modulo in programming languages. Leijen provides the following algorithms for calculating the two divisions given a truncated integer division:[5]

10^9+7 fulfills both the criteria. It is the first 10-digit prime number and fits in int data type as well. In fact, any prime number less than 2^30 will be fine in order to prevent possible overflows.
How modulo is used:
A few distributive properties of modulo are as follows:

Observe that the MMI of a number may be different for different M.
So, if we are performing modulo arithmetic in our program and we need the result of the operation x / y, we should NOT perform

If a number is a multiple of 4, when you divide it by 4 the remainder will be 0. So you would create the logic to take an input and use the mod 4 operation on it. If the result is 0 the number is a multiple of 4 otherwise the number is not a multiple of 4.

If you did not use the mod operator you would have to do the math in your code. For example you would have to calculate "is 496 a multiple of 4?". You would divide 496 by 4, so 496 / 4 = 124 with no remainder. In terms of mod, 496 mod 4 = 0, so yes, 496 is a multiple of 4.

In the two-argument case, the result is roughly the same as (atan (/ (exact->inexact y)) (exact->inexact x)), but the signs of y and x determine the quadrant of the result. Moreover, a suitable angle is returned when y divided by x produces +nan.0 in the case that neither y nor x is +nan.0. Finally, if y is exact 0 and x is a positive number, the result is exact 0. If both x and y are exact 0, the exn:fail:contract:divide-by-zero exception is raised.

The random-seed function is convenient for some purposes, butnote that the space of states for a pseudo-random number generator ismuch larger that the space of allowed values for k. Usevector->pseudo-random-generator! to set a pseudo-randomnumber generator to any of its possible states.

If convert-mode is 'number-or-false, the result is#f if s does not parse exactly as a number datum(with no whitespace). If convert-mode is 'read, theresult can be an extflonum, and it can be a string thatcontains an error message if read of s would reporta reader exception (but the result can still be #f ifread would report a symbol).

Before printing, n is converted to an exact number,multiplied by (expt 10 decimal-digits), rounded, and thendivided again by (expt 10 decimal-digits). The result of thisprocess is an exact number whose decimal representation has no morethan decimal-digits digits after the decimal (and it ispadded with trailing zeros if necessary).

If n is a real number with no decimal representation (e.g.+nan.0, +inf.0), then the exn:fail:contract exception is raised.(Any real number that is convertible to decimal notation is rational,so n must be rational?, despite the name of thefunction.)

In programming, taking the modulo is how you can fit items into a hash table: if your table has N entries, convert the item key to a number, do mod N, and put the item in that bucket (perhaps keeping a linked list there). As your hash table grows in size, you can recompute the modulo for the keys.

As we will see, the big problem with modulo biases is that they can allow you to recover private keys for certain schemes, including the ones used in Bitcoin, Ethereum and many other blockchains, or by manufacturers to sign their firmwares!

Rejection sampling combined with modulo works well, as long as you are very careful with your bounds, because otherwise you might have the same problem as Cryptocat, where they were doing rejection sampling from 0 to 250 inclusive before computing the modulo reduction by 10. This means that they had 251 possible random values instead of 250, and the 251th value, , was mapped to 0 instead of being rejected, and thus they had 2 chances out of 26 to get 0 and 1 chance out of 26 to get any other digit from 0 to 9.

I think I could compute the logarithm modulo each prime and then combine it, but do not know how exactly. Seems similar like problems for Chinese remainder theorem but I cannot find the way how to do it.

You're real close; you do recombine them using the Chinese Remainder Theorem; however the modulus you use aren't the prime factors, but one less (unless a prime factor is repeated; that gets handled slightly differently).

In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another (called the modulus of the operation). Given two positive numbers a and n, a modulo n (often abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor. For example, the expression "5 mod 2" would evaluate to 1, because 5 divided by 2 has a quotient of 2 and a remainder of 1, while "9 mod 3" would evalu...

In most cases this will suffice as most algorithms just want to loop around arrays and get to the other end of the array when doing some sort of simple traversal. For me, it was a ring of LEDs I was animating using the FastLED library.

The LibreTexts libraries are Powered by NICE CXone Expert and are supported by the Department of Education Open Textbook Pilot Project, the UC Davis Office of the Provost, the UC Davis Library, the California State University Affordable Learning Solutions Program, and Merlot. We also acknowledge previous National Science Foundation support under grant numbers 1246120, 1525057, and 1413739. Legal. Accessibility Statement For more information contact us at in...@libretexts.org.

Can someone explain why this is desired behavior? I want to iterate through an array backwards and wrap the index counter around to the last index of the array using modulo. This is pretty common desired behavior of modulo.

In mathematics, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" when reaching a certain value, called the modulus. The modern approach to modular arithmetic was developed by Carl Friedrich Gauss in his book Disquisitiones Arithmeticae, published in 1801. A familiar use of modular arithmetic is in the 12-hour clock, in which the day is divided into two 12-hour periods. If the time is 7:00 now, then 8 hours later it will be 3:00. Simple addition would result ...

In computing, the modulo operation returns the remainder or signed remainder of a division, after one number is divided by another (called the modulus of the operation). Given two positive numbers a and n, a modulo n (abbreviated as a mod n) is the remainder of the Euclidean division of a by n, where a is the dividend and n is the divisor. The modulo operation is to be distinguished from the symbol mod, which refers to the modulus (or divisor) one is operating from. For example, the expression ...

c80f0f1006
Reply all
Reply to author
Forward
0 new messages