Jump to content
  • Advertisement

Archived

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

BlueDev

Find if integer is odd

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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.

My bad.

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster


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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!