• ### What is your GameDev Story?

#### Archived

This topic is now archived and is closed to further replies.

# Find if integer is odd

This topic is 5418 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

i think the lookup table is a good idea, but it would use a huge ammount of memory on each workstation runing ur aplication.

so i suggest keeping the lookup table only on one server to save space. Applications would then use a webservice hosted on the server to know the parity of an int

##### Share on other sites
Try building a lookup table of all the odd numbers within the integer range. Then when you need to determine the odd-ness of a number, search for it in that array.

##### Share on other sites
stupid idea, it would take very long !

using the int as an index in an array is way more efficient !

##### Share on other sites
I have the solution :

bool isOdd( int x )
{
return !isEven(x);
}

bool isEven( int x )
{
return !isOdd(x);
}

And may God save the queen!

##### Share on other sites
I thought I would be a smart-ass and "unroll the loop". So I made a program that looked a bit like this:

int main(){  ofstream out("temp.txt");  for(int i=1; i<INT_MAX; i++)    out << "if(bbq==" << i << ")" << endl << "  return true;" << endl;  out << "return false";  out.close();  return 0;}

Without thinking a great deal, I hit run and watched the file size of temp.txt grow from 0 to about 80MB before I stopped it.

I guess INT_MAX is ~2 Billion, and I was outputting anywhere from 25 to 35 characters per odd int, which would end up being (1 Bil. x ~30 bytes per =) 30GB of data.

[edited by - Rick Scott on March 12, 2004 3:34:28 PM]

##### Share on other sites
You have a bug in you code, see below

int main()
{
ofstream out("temp.txt");
//for(int i=1; i // Cycle only through odd numbers
for(int i=0; i out << "if(bbq==" << i << ")" << endl << " return true;" << endl;
out << "return false";
out.close();
return 0;
}

##### Share on other sites
bool might_be_odd(unsigned long num){  for(int i = 0; i < 10000; ++i)  {     int guess = rand() << 1;     if( guess == num ) return 0;  }  return 1;}

“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

##### Share on other sites
bool isOdd(int x) {   char c = (char)(x%256);   while (c<''a'') c+=26;   while (c>''z'') c-=26;   if ((c==''a'') || (c==''c'') || (c==''e'') ||       (c==''g'') || (c==''i'') || (c==''k'') ||       (c==''m'') || (c==''o'') || (c==''q'') ||       (c==''s'') || (c==''u'') || (c==''w'') ||       (c==''y''))       return true;   else       return false;}

##### Share on other sites
bool isOdd( unsigned int value ){    if (cos( (float)value * 3.1415926 )<0)        return true;    return false;}

Only works for numbers below 29,311,484 ....

##### Share on other sites
Hi all,

I really appreciate Inmate2993''s example of finding if an integer is odd. Not only can I find out if it is, I can also find out if its not. I was about to ask how to make a isEven too but I won''t need that anymore

bool isOdd(int x) {  if(x >= 0)    return (x&1);  else    return ((- x)&1);}

// Elsewhere
if( isOdd(myNum) )
// or
if( !isOdd(myNum) )

Just my opinion though,
[BDS]StackOverflow

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 11
• 9
• 9
• 38
• ### Forum Statistics

• Total Topics
634129
• Total Posts
3015701
×