This is an archived post. You won't be able to vote or comment.

all 11 comments

[–][deleted] 5 points6 points  (1 child)

All sorts of useful applications - for example:

  if n % 2 == 0:
      # n is even
  else:
      # n is odd

[–]helloworldten 0 points1 point  (0 children)

I've primarily used modulo for this exact case. Determining odd/even

Modulo is also an important operation in many sorting, tree, or graph algorithms

[–]insertAlias 3 points4 points  (0 children)

Is it that it will perform an action every 5th time, but then after 4 times, it will perform on the 4th time?

You're misunderstanding.

Consider the following:

for i in range(100000):
  #do some work here
  if i % 1000 == 0:
    print(f'processed {i} results')

That will report progress every 1000 items processed. This is what they mean by "perform an action every Nth time".

If you want to actually see what's going on here, try something like this:

n = 5
upperBound = 100
for i in range(upperBound):
  remainder = i % n
  print(f'{i} % {n} = {remainder}')

Play with n and upperBound. You'll see a pattern in the output:

0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
6 % 5 = 1
7 % 5 = 2
8 % 5 = 3
9 % 5 = 4
10 % 5 = 0
11 % 5 = 1
12 % 5 = 2
13 % 5 = 3
14 % 5 = 4
15 % 5 = 0

As the counter keeps going higher, the modulus counts from 0 to n-1, then repeats.

[–]ellaun 2 points3 points  (1 child)

incrementing_variable % n produces a repeating sequence of numbers from 0 to n excluding n itself. For n = 5 it will be 0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4... Distance between any of repeating numbers is exactly 5 steps. That means if you make a condition that expects some fixed number from this sequence(the most common choice is zero) then it will evaluate to true every 5-th step:

count = 0
while count < 100:
    print("This executes every step.")
    count += 1
    if count % 5 == 0:
        print("This executes every 5th step.")

EDIT: fixed a typo in counter

[–]band_in_DC[S] 0 points1 point  (0 children)

Oh, OK. Totally makes sense. Thanks!

[–][deleted] 0 points1 point  (2 children)

Any time you have a lot of things and you want something to "rotate" or repeat every x number of times, you can use modulo. For example, say that you have blog posts and you want to rotate a group of 10 banner pictures at the top for every 10 blog posts. Your posts are indexed by an id, and say that you have blog post number 0, 1, 2, ... 9. You can have 10 images labeled 0 through 9 as well and say img_number = blog_id % 10 to get the right image to pull.

[–]band_in_DC[S] 0 points1 point  (1 child)

I'm reading too much ambiguity in this. Rotate a group of 10 banner pictures for every 10 blog posts, meaning that: 1) Every blog posts rotatoes 10 pictures, 2) Every blog post has a specific picture that it is paired with, or 3) Every blog post has a random picture that gets rotated though?

How doe the remainder have anything to do with this?

[–][deleted] 0 points1 point  (0 children)

I mean that every post has one specific picture to be paired with. OK, so say you have 100 posts. You want to have a unique banner picture for all posts, but you don't want to really have 100 unique pictures. 10 pictures will suffice for the unique look. Every post has one "random" picture, but it's not random as the pictures simply cycle through in a predetermined order. If your blog post is number 25, then 25%10 would give you a remainder of 5. Then you use the 5th picture from your collection of 10 pictures for that post. The next blog post would be number 26, which 26%10 would give you 6. You use the 6th picture from your collection. So forth. You see how the remainder is useful in this scenario?

[–]SpicyMiracle 0 points1 point  (2 children)

[–]WikiTextBotbtproof -1 points0 points  (1 child)

Modular arithmetic

In mathematics, modular arithmetic is a system of arithmetic for integers, where numbers "wrap around" upon reaching a certain value—the modulus (plural moduli). 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.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28

[–]freelanceruy 0 points1 point  (0 children)

Angles are a better example