Find if integer is odd

Started by
207 comments, last by BlueDev 20 years, 1 month ago
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
Advertisement
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.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
stupid idea, it would take very long !

using the int as an index in an array is way more efficient !
I have the solution :

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

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

And may God save the queen!
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.

My bad.

[edited by - Rick Scott on March 12, 2004 3:34:28 PM]
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;
}
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
"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
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;} 
bool isOdd( unsigned int value ){    if (cos( (float)value * 3.1415926 )<0)        return true;    return false;} 

Only works for numbers below 29,311,484 ....
--------<a href="http://www.icarusindie.com/rpc>Reverse Pop Culture
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
[/quote]

This topic is closed to new replies.

Advertisement