# A Mechanical Mod Function

So if you are probably wondering what a mod function is. It is simply the remainder. For example, 5 mod 3 is 2. If the 2nd number cleanly divides the first number, then the result is 0. So 30 mod 6 is 0. Here is a practice problem:

What is 20 mod 4?

Now that we know what it is, we can now start figuring out how to build it. Please don’t go because it looks really long. It isn’t. I’m just explaining how to find out how to make it, and not just giving you the device system.

So first, “mod function” is a bit too vague for us to just spontaneously build. So we need to specify more. Here is the question we will need to answer first:

How do we systematically find the remainder?

There’s a few options:

• Divide the two numbers and use the floor function to find the remainder

Now this is not what we want because this will definitely require blocks. So we need another approach.

• Repeatedly subtract the 2nd number from the first.

Now this is definitely possible by using negative item granters or subtracting using counters. We also have recursion for the repeating. This will not require any blocks, so this is the way to go! If you have any other ideas, feel free to make a guide on it or put it in the comments if you want me to make it!

So we now have 2 approaches: the item granter one and the counter one. If I’m right a the counter is only 15 memory (I’m on mobile), then the counter method will be better than the item granter one because the counter uses less memory.

So I’ve highlighted a counter and an item granter method. Can you guess the reasons why?

Reasons

Only counters and IIMs can edit properties, other than blocks. IIMs don’t really do anything very useful, so its the item granter we use.

First, get the number you want to have after the “mod”. If its 232 mod 3, then its 3. Lets call that the base. Base is a math term and you don’t need to know that to do this guide.

Now, lets start with the item granter method. The first step is to give number of items the property has to the player. But to do this, you will either have to use blocks, or use too many counters. So this method is now ruled out. We only have the counter method left. Let’s hope that that isn’t like the item granter method. But how do we make the counter start at the property? So now we have hit a barrier. It seems like this guide should be deleted and be sent to devices. But wait! There’s a hope! We can call this a restriction of the mechanical mod function. So basically, the property has to start as either modified by the counter or modified by the IIM. Now we can get rolling!

So for the item granter method again. Now, how do we repeatedly subtract the number? That’s right! use recursion on a negative item granter granting the negative base. So lets set up the recursion. I’m not sure if recursion works with channels, so I’ll use a wire repeater. Wire the item granter to the wire repeater and the wire repeater back to the item granter. Now we have a recursing negative item granter! To start this, add a wire going into the wire repeater.

All done, right? Lets test this! Make the property broadcast to a block in a text device. It should set the text to the property.

Oh no! When we test this, the property becomes negative, and that’s not what we want! This is actually the result of a bit more vagueness. Look at this:

I forgot to say that it is until the result becomes less than the base. The bug was a result of vagueness! So how do we implement that? We use the checker! Delete the wire from the wire repeater to the item granter. Place a checker checking for the property. Wire the wire repeater to the checker and the checker to the item granter. It should be on check passes. When we test this, this should work!

Now the counter method will come soon!

5 Likes

No idea why I personally need the mod function as I have hardly ever used it while programming outisde of GKC, but good guide!

It’s not finished yet, and the mod function is used a lot in number theory, and as you might infer from experience with GKC, in complex CS.

Tbh I always forget about it and only use it with lists.

Nice job! This is actually pretty interesting!

1 Like

Thanks! I’m planning to make all my guides like this from now on.

The biggest use that I can come up with is easily making a clock.

Item granter method done!