Script for determining the sum of all values is odd or even?


#1

Hi, is it possible to have a script to determine the sum of all added numbers is odd or even ? without having to compare to every odd or even number in the range ?


#2

It should be possible, but off the top of my head it would taking some coding. I am sure @CJW or a few of others that understand higher function coding could give you a better answer.


#3

You should be able to use the modulo operator to do this (%). It gives the remainder when dividing by something, so dividing an integer by two will always yield either zero or one: zero meaning even, one meaning odd.

An example:

*if (((var1 + var2) + var3) % 2) = 0
	This is even.
	*goto specialevenstuff
*else
	The sum is odd.
	*goto specialoddstuff

You might already know this, but keep in mind that all of the parentheses are needed, though it might seem excessive: you can only do something to two things at once, and then you need parentheses around it.
For example, if you have 13 variables (which is probably an excessive number):

(((((((((((((var1 + var2) + var3) + var4) + var5) + var6) + var7) + var8) + var9) + var10) + var11) + var12) + var13) % 2)

Or, in another way:

(((((var1 + var2) + (var3 + var4)) + ((var5 + var6) + (var7 + var8))) + (((var9 + var10) + (var11 + var12)) + var13)) % 2)

Both are correct, being two of many other ways to write it, but what’s important is that there are always the pairs within parentheses, each parenthetical enclosure counting as its own unit.

I hope this helps!


#4

Thank you so much, that has been extremely helpful !


#5

The only additional suggestion I’d make is to add the numbers and then use the modulo operator on the sum, i.e. don’t try and cram 8 variable additions into an *if statement, otherwise it gets messy and hard to read.