# Creating Repeated Sequences With The Modulus (MOD) Operator

Posted August 10, 2011 at 9:49 AM by Ben Nadel

Tags: ColdFusion

This almost doesn't warrant its own post; but, it seems to be one of those small factoids that I can't keep in my head without writing it down. And so, a quick post to demonstrate creating repeated sequences with the modulus operator (MOD or %). To be fair, the modulus operator already creates repeated sequences; but, those sequences end with a zero. This is perfect for when you only care about one number in a particular sequence; but, when you want to deal with a sequence as a set of numbers, typically, you want your sequence to start at 1 and proceed to N.

In order to end your sequence on "N", you have to add some math to your modulus calculation. But, before we look at that, let's take a look at a simple modulus loop:

• <!---
• Loop to 15 using a modulus of 3 to get a result
• at each index.
• --->
• <cfloop
• index="i"
• from="1"
• to="15"
• step="1">
•
• #i# % 3 = #(i % 3)#<br />
•
• </cfloop>

Here, we are looping from 1 to 15 and outputting the mod-3 for each index. When we run this code, we get the following output:

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

As you can see, this creates the repeated sequence, {1,2,0}. Like I said, standard modulus sequences end with zero. This is great if you want to determine which index you are at (1 == first, 0 == last); but, if you wanted to get the index within the repeated sequence, your logic gets a bit more tricky.

To create a repeated sequence that ends in N rather than zero, we have to add some more math to the mix:

• <!---
• Now, loop to 15 using a modulus of 3. However, this time,
• rather than repeating 1,2,0, we want to repeat 1,2,3. To do
• this, we have to use a slightly different formula:
•
• ((n - 1) % 3) + 1
• --->
• <cfloop
• index="i"
• from="1"
• to="15"
• step="1">
•
• ((#i# - 1) % 3) + 1 = #(((i - 1) % 3) + 1)#<br />
•
• </cfloop>

This time, we are subtracting one from the index and then adding one to the result. When we run this code, we get the following output:

((1 - 1) % 3) + 1 = 1
((2 - 1) % 3) + 1 = 2
((3 - 1) % 3) + 1 = 3
((4 - 1) % 3) + 1 = 1
((5 - 1) % 3) + 1 = 2
((6 - 1) % 3) + 1 = 3
((7 - 1) % 3) + 1 = 1
((8 - 1) % 3) + 1 = 2
((9 - 1) % 3) + 1 = 3
((10 - 1) % 3) + 1 = 1
((11 - 1) % 3) + 1 = 2
((12 - 1) % 3) + 1 = 3
((13 - 1) % 3) + 1 = 1
((14 - 1) % 3) + 1 = 2
((15 - 1) % 3) + 1 = 3

As you can see, this time, we created the repeated sequence, {1,2,3}. With the extra math, we were able to end on N (3 in our case) rather than zero.

And, of course, we could abstract this concept out into its own function:

• <cffunction
• name="modSequence"
• access="public"
• returntype="numeric"
• output="false"
• hint="I return the modulus result of the given value into the given sequence such that the resultant sequence goes from 1 to N (rather than zero).">
•
• <!--- Define arguments. --->
• <cfargument
• name="index"
• type="numeric"
• required="true"
• hint="I am the index that we will fit into the given sequence."
• />
•
• <cfargument
• name="sequence"
• type="numeric"
• required="true"
• hint="I am sequence (max value) into which we will be fitting the given index."
• />
•
• <cfreturn (((arguments.index - 1) % arguments.sequence) + 1) />
• </cffunction>
•
•
• <!--- loop to 15, repeating 1,2,3. --->
• <cfloop
• index="i"
• from="1"
• to="15"
• step="1">
•
• modSequence( #i#, 3 ) = #modSequence( i, 3 )#<br />
•
• </cfloop>

Here, we're simply encapsulating the details behind a function invocation. When we run the above code we get the following page output:

modSequence( 1, 3 ) = 1
modSequence( 2, 3 ) = 2
modSequence( 3, 3 ) = 3
modSequence( 4, 3 ) = 1
modSequence( 5, 3 ) = 2
modSequence( 6, 3 ) = 3
modSequence( 7, 3 ) = 1
modSequence( 8, 3 ) = 2
modSequence( 9, 3 ) = 3
modSequence( 10, 3 ) = 1
modSequence( 11, 3 ) = 2
modSequence( 12, 3 ) = 3
modSequence( 13, 3 ) = 1
modSequence( 14, 3 ) = 2
modSequence( 15, 3 ) = 3

The modulus operator is pretty awesome, no doubt. Hopefully, I'll now be able to remember how to use it to create a more "standard" repeated sequence. And if I can't, at least I know where to look.

### Looking For a New Job?

25% of job board revenue is donated to Kiva. Loans that change lives - Find out more »

Aug 10, 2011 at 11:10 AM // reply »

When I first started programming, I thought the MOD operator was the coolest, most awesome and interesting thing. I still love it to this day. Thanks for writing about it. You get extra brownie points, as well, for adding extra math. :-)

Aug 10, 2011 at 1:35 PM // reply »

@Anna,

Yeah, the mod operator is totally awesome :) It's one of my favorite operators (which may very well be the geekiest thing I say all day).

Aug 10, 2011 at 3:13 PM // reply »

@Ben,

haha...you can be geeky, that's ok. I am geeky all day, every day, probably, but it's gotten so bad with me, that I don't even really notice it anymore, and don't recognize it when I am.

Aug 10, 2011 at 7:43 PM // reply »

@Ben,

If you cfloop from -6 to 6, instead of from 1 to 15, you'll see that % is actually a remainder operator, not a true modulo. In a true modulo, -5 % 3 would 1, not -2.

It can make a difference if you're trying to generate a random number within a range. Say you want to generate a random number in the range of 0 to 59, to represent the number of seconds to wait before doing something. Ordinarily, % 60 does that quite nicely. But if the first operand accidentally goes negative somehow, you instead get a number in the range of -59 to 0.

Just something to watch out for.

Aug 10, 2011 at 8:02 PM // reply »

This might be a bit more visual. Switching to code for its monofont.

• Remainder versus true modulo:
•
• i i \ 3 i % 3 true i mod 3
•
• -6 -2 0 0
• -5 -1 -2 1
• -4 -1 -1 2
• -3 -1 0 0
• -2 0 -2 1
• -1 0 -1 2
• 0 0 0 0
• 1 0 1 1
• 2 0 2 2
• 3 1 0 0
• 4 1 1 1
• 5 1 2 2
• 6 2 0 0

You can take integer quotient times divisor and add in the remainder to get the dividend, on either side of 0.

You can't do that with true modulo.

Aug 10, 2011 at 8:03 PM // reply »

Aw, shucks. I wish you would allow pre in addition to code.

Aug 15, 2011 at 11:41 AM // reply »

@Ben, I forgot to mention that I like the other operators, too. It's just that by the time I got to the mod operator, I had used the others so much, I had just grown kind of tired of them. They were kind of like old news at that point. But I still like them. Just think the mod is super-cool. (and can be helpful for a lot of solutions to different problems you can't really solve easily in other ways).

Aug 18, 2011 at 9:41 AM // reply »

We use MOD to place things in row, three columns wide. Thus, if there are eight items, the output ends up looking like:
1 2 3
4 5 6
7 8
is that what prompted this post, Ben?

Oct 29, 2011 at 10:02 PM // reply »

@WebManWalking,

It's funny you mention that - I almost never think about numbers in the negative. I know that sounds odd; but, I can't even remember the last time that I really had to work with negative numbers. ... I am sure that is a ridiculous thought - I am sure I use them all the time :)

@Randall,

That's the kind of situation where I really started to use MOD for the first time on a regular basis. When I was using Table tags to lay things out, especially, I remember I used to use the MOD operator to determine where I should add the closing / opening TR tags.

Feb 13, 2013 at 3:22 PM // reply »

Another particularly useful way to use the MOD operator is to break items up into an arbitrary number of columns. Say you have an unknown number of elements and you want to display them in three reasonably even columns, you would divide the number of elements by the number of columns, round up or down (so you're working with an integer), and start a new column when your MODULUS is zero.

Feb 14, 2013 at 9:51 AM // reply »

@Paul,

I believe that splitting columns and rows was actually one of the first things I ever used it for in production. I vaguely remember having code like:

• <tr>
• <cfloop query="foo">
•
• <td> .. </td>
•
• <cfif ! ( foo.currentRow mod 3 )>
•
• <!-- Start new row. -->
• </tr><tr>
•
• </cfif>
•
• </cfloop>
• </tr>

... something like that.

Feb 14, 2013 at 10:38 AM // reply »

@Ben
That's about what the code would look like. I was thinking about, say, three left-floating DIVs with unordered lists containing the items you want to display. In that situation, your modulus divisor is not constant, but calculated based on the number of records.

I suppose you could also do this sort of thing with news articles or classified ads based on the number of "inches" for each article or some other arbitrary unit of measuring length, thus simulating the columnar layout of a newspaper.

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

 Author Name: Author Email: Author Website: Comment: Supported HTML tags for formatting: bold   italic   code Remember my information Subscribe to comments Send me a copy of this comment
InVision App - Prototyping Made Beautiful With Prototyping Tools Recent Blog Comments
Mar 7, 2014 at 8:31 PM
Sanity Check: \$index vs. DOM In AngularJS Directives
I had NOOOO idea you could pass the entire friend object into a method like this: removeFriend( friend ) . I was always using some ID of the object and passing that around back and forth between vie ... read »
Mar 7, 2014 at 10:43 AM
Project HUGE: Active Release Technique (ART) With Dr. Christopher Anselmi In NYC
Does anyone know of a GOOD A.R.T. Provider near the Binghamton,NY area? I have gone to a couple of people that said they do ART but they don't. It's just a massage. Thanks for any help you can give ... read »
Mar 7, 2014 at 8:44 AM
GMail Seems To Ignore The Return-Path Header Defined By The CFMail FailTo Attribute
So, is the header "Problems-to" no longer valid? I also add "return-path" via cfmailparam. ... read »
Mar 6, 2014 at 8:58 PM
Posting XML With ColdFusion, CFHttp, And CFHttpParam
ERROR2: Missing type node :( ... read »
Mar 6, 2014 at 6:56 AM
CFLoop Attributes Evaluated Only Once
Hi Ben, An "oldie but a goodie"... funny enough we just had a discussion about this at work, what with JavaScript evaluating at each iteration of a for loop it stems to reason that CF woul ... read »
Mar 5, 2014 at 6:27 AM
Defining Instantiatable Classes In The AngularJS Dependency Injection Framework