Recognizing when to use the modulus operator

前端 未结 19 807
醉梦人生
醉梦人生 2020-11-29 16:40

I know the modulus (%) operator calculates the remainder of a division. How can I identify a situation where I would need to use the modulus operator?

I know I can u

相关标签:
19条回答
  • 2020-11-29 17:03

    I use it for progress bars and the like that mark progress through a big loop. The progress is only reported every nth time through the loop, or when count%n == 0.

    0 讨论(0)
  • 2020-11-29 17:09
    0 % 3 = 0;
    1 % 3 = 1;
    2 % 3 = 2;
    3 % 3 = 0;
    

    Did you see what it did? At the last step it went back to zero. This could be used in situations like:

    1. To check if N is divisible by M (for example, odd or even) or N is a multiple of M.

    2. To put a cap of a particular value. In this case 3.

    3. To get the last M digits of a number -> N % (10^M).
    0 讨论(0)
  • 2020-11-29 17:10

    Example. You have message of X bytes, but in your protocol maximum size is Y and Y < X. Try to write small app that splits message into packets and you will run into mod :)

    0 讨论(0)
  • 2020-11-29 17:10
    • Computing the greatest common divisor
    • Determining if a number is a palindrome
    • Determining if a number consists of only ...
    • Determining how many ... a number consists of...
    0 讨论(0)
  • 2020-11-29 17:13

    Modulus is also very useful if for some crazy reason you need to do integer division and get a decimal out, and you can't convert the integer into a number that supports decimal division, or if you need to return a fraction instead of a decimal.

    I'll be using % as the modulus operator

    For example

    2/4 = 0

    where doing this

    2/4 = 0 and 2 % 4 = 2

    So you can be really crazy and let's say that you want to allow the user to input a numerator and a divisor, and then show them the result as a whole number, and then a fractional number.

    whole Number = numerator/divisor
    fractionNumerator = numerator % divisor
    fractionDenominator = divisor
    

    Another case where modulus division is useful is if you are increasing or decreasing a number and you want to contain the number to a certain range of number, but when you get to the top or bottom you don't want to just stop. You want to loop up to the bottom or top of the list respectively.

    Imagine a function where you are looping through an array.

    Function increase Or Decrease(variable As Integer) As Void
        n = (n + variable) % (listString.maxIndex + 1)  
        Print listString[n]
    End Function
    

    The reason that it is n = (n + variable) % (listString.maxIndex + 1) is to allow for the max index to be accounted.

    Those are just a few of the things that I have had to use modulus for in my programming of not just desktop applications, but in robotics and simulation environments.

    0 讨论(0)
  • 2020-11-29 17:13

    Best use of modulus operator I have seen so for is to check if the Array we have is a rotated version of original array.

    A = [1,2,3,4,5,6] B = [5,6,1,2,3,4]

    Now how to check if B is rotated version of A ?

    Step 1: If A's length is not same as B's length then for sure its not a rotated version.

    Step 2: Check the index of first element of A in B. Here first element of A is 1. And its index in B is 2(assuming your programming language has zero based index). lets store that index in variable "Key"

    Step 3: Now how to check that if B is rotated version of A how ??

    This is where modulus function rocks :

    for (int i = 0; i< A.length; i++)
    {
    
    // here modulus function would check the proper order. Key here is 2 which we recieved from Step 2
       int j = [Key+i]%A.length;
    
       if (A[i] != B[j])
       {
         return false;
       }
    }
    
    return true;
    
    0 讨论(0)
提交回复
热议问题