Jump to content

Modulus


Ninjakreborn

Recommended Posts

Well, if those are the ONLY values you want to check, then what you have will suffice. You could use modulus to check for each 5th instance, but then you need to add minimum and maximum validations. So the number of validations is not that different

 

if($num>=1 && $num<=21 && $num%5==1) {

 

Even though that only requires 3 checks as opposed to your original of 5, I would prefer your original code for the sake of readability. I can easily see what numbers the validation allows looking at your original code, whereas for the one using modulus it take some mental calculations. But, if you have a much wider minimum/maximum values then modulus would make sense.

Link to comment
Share on other sites

Yes, the ultimate value I have set it at now is 201.  I might even expand that to closer to 500. This works out really well though. If it was

just a few numbers and ONLY that I would leave it, but I had an old mentor of mine once taught me about Modulus in a similar situation..so I started

thinking of him again and was trying to remember how to do it. That helped a lot, thanks again man.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.