## Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

# Even or Odd integer detection.

Chris Knox
Member
Join date: 22 Apr 2004
Posts: 40
11-08-2004 14:53
Someone once asked me in a sandbox if I knew how to detect even or odd. I couldn't think of it then, and I forgot the person's name, but here it is now. Hoping this can help anyone. If you want help adapting this for your script just IM me in world.

CODE
`//Even/Odd from Chris Knoxfloat n;float m;default{    state_entry()    {        n = 0;            }    touch_start(integer total_number)    {        m = (n + 1);                float number = n;                float tell = llRound(number / 2);                if ((tell * 2) == number)        {            llSay(0, (string)llRound(number) + " is even.");        }        else        {            llSay(0, (string)llRound(number) + " is odd.");        }                n = m;                    }}`
Zuzi Martinez
goth dachshund
Join date: 4 Sep 2004
Posts: 1,860
11-09-2004 02:52
you can also do it this way. my script is probably sloppy but the method seems to work alright.
CODE
`string OddOrEven(integer n) {    if (!n)   return "i dunno if zero is odd or even.";    integer temp = n % 2;    if (temp) return (string)n +" is odd.";    else      return (string)n +" is even.";}default {    state_entry() {        // replace 11 with whatever number you want to check.        llSay(0, OddOrEven(11));    }}`
Neo Rebus
Registered User
Join date: 10 Apr 2004
Posts: 59
11-09-2004 07:26
It's much easier:

if (number % 2 == 0) { llSay(0, (string)number + " is even."; }
if (number % 2 == 1) { llSay(0, (string)number + " is odd."; }

From: Chris Knox
Someone once asked me in a sandbox if I knew how to detect even or odd. I couldn't think of it then, and I forgot the person's name, but here it is now. Hoping this can help anyone. If you want help adapting this for your script just IM me in world.

CODE
`//Even/Odd from Chris Knoxfloat n;float m;default{    state_entry()    {        n = 0;            }    touch_start(integer total_number)    {        m = (n + 1);                float number = n;                float tell = llRound(number / 2);                if ((tell * 2) == number)        {            llSay(0, (string)llRound(number) + " is even.");        }        else        {            llSay(0, (string)llRound(number) + " is odd.");        }                n = m;                    }}`
Apotheus Silverman
I write code.
Join date: 17 Nov 2003
Posts: 416
11-09-2004 09:31
// Returns TRUE if testNumber is even, FALSE otherwise
integer IsEven(integer testNumber) {
if (testNumber % 2 == 0) {
return(TRUE);
}
return(FALSE);
}
_____________________
Apotheus Silverman
Shop SL on the web - SLExchange.com

Visit Abbotts Aerodrome for gobs of flying fun.
Xylor Baysklef
Join date: 4 May 2003
Posts: 109
11-09-2004 10:04
Or, you could use the modulus operator:

(pseudo code)
CODE
`if (some_integer % 2 == 0) then some_integer is evenelse some_integer is odd`

Xylor
Lorn Prefect
Second Life Resident
Join date: 4 Nov 2004
Posts: 4
11-09-2004 20:19
Simpler method given 'integer n'

integer isOdd = n & 1;

This does a bitwise comparison; if the number is odd, the low bit (1) is set - no even number can have the low bit set, since the rest of the bits are all evens, and even+even number = even.
Lorn Prefect
Second Life Resident
Join date: 4 Nov 2004
Posts: 4
11-09-2004 20:45
Try this... given 'integer n':

integer isOdd = n & 1;

This will do a bitwise comparison; if a number is odd, the low bit (the only odd bit - 1), will be set.
Lorn Prefect
Second Life Resident
Join date: 4 Nov 2004
Posts: 4
Try bitwise...
11-09-2004 20:45
Try this... given 'integer n':

integer isOdd = n & 1;

This will do a bitwise comparison; if a number is odd, the low bit (the only odd bit - 1), will be set.
Lorn Prefect
Second Life Resident
Join date: 4 Nov 2004
Posts: 4
11-09-2004 22:52
Even better... Given 'integer n':

integer isOdd = n & 1;

This will do a bitwise comparison; if a number is odd, it's lowest bit (1) is set.
Lorn Prefect
Second Life Resident
Join date: 4 Nov 2004
Posts: 4
11-09-2004 22:53
Even better... Given 'integer n':

integer isOdd = n & 1;

This will do a bitwise comparison; if a number is odd, it's lowest bit (1) is set.

Given float n, cast it to an integer:

integer isOdd = (integer)n & 1;

Floats cast to integers drop the decimal. If you want it rounded to nearest:

integer isOdd = (integer)(n + 0.5) & 1;
Samhain Broom
Registered User
Join date: 1 Aug 2004
Posts: 298
11-12-2004 12:50
From: Lorn Prefect
Even better... Given 'integer n':

integer isOdd = n & 1;

This will do a bitwise comparison; if a number is odd, it's lowest bit (1) is set.

Given float n, cast it to an integer:

integer isOdd = (integer)n & 1;

Floats cast to integers drop the decimal. If you want it rounded to nearest:

integer isOdd = (integer)(n + 0.5) & 1;

In some languages, the "n" will be ANDed with the 1, and the result "isOdd" will be TRUE or FALSE but the value of "n" will still be changed. If "n" is a "throw away" variable, that's not a problem. The method used by Xylor does not change the variable. I do not mean to be picky, but if you were tight on memory, the Modulo version of "Even/Odd" discovery is best.

And of course this applies to any value. IE, is it divisible by x?

llWhisper(0, "Divisible by " + (string)x) && (number % x) == 0;

Hmmm well, a similar method like that works in PERL, =)
_____________________
rm -rf /bin/ladden #beware of geeks bearing grifts
Jack Wishbringer
Second Life Resident
Join date: 9 Nov 2004
Posts: 41
11-15-2004 06:40
Just a minor point in response to the last post; but "n & 1" will not store the result back in n; the value of n is preserved.

To store the result back in n, you'd use n = n & 1; or n &= 1;.

Jack