Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Even/Odd Integer?

Douglas Callahan
Fresh Prince Of SL
Join date: 2 Jul 2004
Posts: 349
03-26-2005 12:20
I was wondering whether or not there is a way to determine if an integer is even or odd. Couldn't find anything on wiki about this.
Aimee Weber
The one on the right
Join date: 30 Jan 2004
Posts: 4,286
03-26-2005 12:23
I'm not a scripter but did you try dividing it by 2 and seeing if there was a remainder?
_____________________
Adam Zaius
Deus
Join date: 9 Jan 2004
Posts: 1,483
03-26-2005 12:24
CODE
if(num % 2) { 
llSay(0,'Odd');
} else {
llSay(0,'Even');
}
_____________________
Co-Founder / Lead Developer
GigasSecondServer
Douglas Callahan
Fresh Prince Of SL
Join date: 2 Jul 2004
Posts: 349
03-26-2005 12:25
duh! :p Sorry guys, having an off day. Thanks for the help. :rolleyes:
Talena Wallaby
Registered User
Join date: 20 Mar 2005
Posts: 19
03-26-2005 15:19
Alternatively:

CODE
if (num & 1)
{
llSay(0, "Odd");
}
else
{
llSay(0, "Even);
}


Bit masking would seem to me to be slightly faster, but I've not tested to find out.
Carnildo Greenacre
Flight Engineer
Join date: 15 Nov 2003
Posts: 1,044
03-27-2005 00:35
How about this:
CODE

integer IsEven(integer i)
{
if(i == 1)
return 0;
else
return IsOdd(i - 1);
}

integer IsOdd(integer i)
{
if(i == 1)
return 1;
else
return IsEven(i - 1);
}
_____________________
perl -le '$_ = 1; (1 x $_) !~ /^(11+)\1+$/ && print while $_++;'
Francis Chung
This sentence no verb.
Join date: 22 Sep 2003
Posts: 918
03-27-2005 00:52
From: Carnildo Greenacre
How about this:


LOL - you must be a Lisp hacker ;)
_____________________
--
~If you lived here, you would be home by now~