Odd or Even - No If Statement

March 1, 2010 at 21:41:42
Specs: Windows Vista
I have an assignment to determine whether a
number is odd or even using ARITHMETIC
only. Doesn't have to use a programming
language, just:

+, -, *, /, %

How can this be done without conditional

See More: Odd or Even - No If Statement

Report •

March 1, 2010 at 21:46:23
You can always mod 2 the number and then check if it's zero. Of course that still needs the conditional.

Since you don't specify it's hard to tell....

Since this is an assignment I'm not going to give you the answer, only these hints:

1. Array index.
2. http://en.wikipedia.org/wiki/Modulo...

Report •

March 1, 2010 at 21:49:37
We are using JavaScript in the class but it can be written in
pseudo-code or anything really. The point is to use the Mod and
simple arithmetic.

I know how to do stuff like this with if-else and loops, but how
can it be done without these?

Report •

March 1, 2010 at 21:53:55
I see you replied as I changed my mind and reply(your quick!).

Do you consider a "while loop" conditional?(which it really is).

Also what do you want to do when if it's odd or even?

Report •

Related Solutions

March 1, 2010 at 21:58:44
Can't use a loop of any sort, only arithmetic and assignment

When it is odd, I am to multiply it by 3 and add 1.

When it is even, I am to divide it by 2. This is all after
determining if it is odd or even using only arithmetic.

We can also use Math.floor, Math.ceil, Math.max, Math.min
but the prof said they aren't necessary.

We have not learned what an array index is (therefore aren't
allowed to use :( ).

I can also define as many variables as I want to make this

I know doing num % 2 will tell me if it is odd or even, but how
can I use that to "route" the number to the correct operations
without using if-else or loops? Just doesn't seem possible to

Report •

March 1, 2010 at 22:22:26
Since this is homework I really don't want to give you the answer, but I can tell you that if the "*3+1" for odd and "/2" is all that needs to be done then yes it can be done, all in a single sum(with some brackets for grouping).

Think along the lines of:

(n % 2) * ((n % 2) * ..... + ((n +1) % 2) * .......

Report •

March 1, 2010 at 22:28:21
Okay, so I'm determining whether it is odd or even and
performing the operations with the same code? Because the
code runs differently based on the starting value?

I see how it is supposed to work, I just don't see how to
implement it?

Report •

March 1, 2010 at 22:41:21
I really want to figure this out myself, but it is almost 1 here and I
have class at 7. Could you possibly nudge me a bit closer? I'm
really not even close to seeing it.

Report •

March 1, 2010 at 22:42:10
I see how it is supposed to work, I just don't see how to
implement it?

I don't do java or javascript so either someone else can help or you may have to figure it out yourself.

Report •

March 1, 2010 at 22:44:54
looks like semantics to me, how to define "determine".
computers generally use either integer 255 (-1) or 0 as "not".
but that's not math, that's computers (boolean algebra).
(warning, i know very little of which i speak, I'm floating this as a means of being flamed / corrected into knowledge!!!)
in an algebraic formula, you would alter the base by the modulo of two, (i think), by multiplication, so that zero gives the number and 1 gives zero, or the opposite (easily arranged)
here is test:
outp=outp*(num % 2)
will yield unaltered output if num is odd, or zero if num is even.
simple enough!

I wondered about this: mathematically to make all integers not zero = 1, (in algbraic formula), and failed, but i haven't googled it, maybe it's simple! Division by zero craps it every time. ie: boil every number down to either 0 or 1. zero is zero, not zero is one.

with computers, you can fudge it because either zero is "yes" , not-zero is "no" or not-zero is "yes" and opposite.

(oops, coffee break! crossed posts in mail!)

Report •

March 1, 2010 at 22:45:25
I didn't mean implement it in javascript, it is supposed to be
submitted in non-programmed form. Just arithmetic.

Report •

March 1, 2010 at 22:51:27
Could you possibly nudge me a bit closer?

Only a little bit closer.

(odd) + (even)

Your logic comes from "(n % 2) * " or "((n +1) % 2) *". "0 *" anything is zero and "1 *' anything is the same as it always was.

         (odd)          +            (even)
((n % 2) * ........ ) + (((n + 1) % 2) * .......) 

Report •

March 1, 2010 at 22:52:41
Ok, thanks. Think I got it from here.

Report •

March 1, 2010 at 23:11:55
Ewww, I think I just did someones homework.........

Report •

March 1, 2010 at 23:29:05
still, i am befogged...
here is test:
a=((num % 2)*num)/2*(num%2)*3+(num % 2)
i will have to see, my math sucks!
also could be algebraically reduced i thinks.

some logic operations

ps: what school you going to dude?!
man, we had nothing like that... but then again, i went to "trade" school. glad to know standards are not deteriorating altogether!.
conversion of algorithm to formula is always tough.

Report •

Ask Question