Explain me the Modulo function


#1

Hello guys,

I know how to use the Modulo function but I can't seem to figure what does it really do. Can anyone please describe me that function with explanation.

Thank you :slight_smile:


#2

Hi, @luciferhere ,

Modular arithmetic relates to remainders or cycles.

Let's assume we have some containers and items, and each container can hold a specific number of items. If the items are eggs, and the containers are egg cartons, each egg carton can hold 12 eggs when it is full.

Suppose we have 42 eggs, as well as some egg cartons, and we only sell eggs in cartons that are full. If we fill as many cartons as we can with our 42 eggs, and sell the full cartons, along with the eggs that they contain, how many eggs remain?

print(42 % 12)

Output:

6

The remainder is 6, and that represents the number of eggs that remain.

We can also consider minutes and seconds, using integer and modular arithmetic. The minutes are the containers, with each one holding 60 seconds. In terms of minutes and seconds, how long is 1000 minutes?

print(1000 // 60)
print(1000 % 60)

Output ...

16
40

1000 seconds is 16 minutes and 40 seconds.


#3

Thank you. That explains really much :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.