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

statements??

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...

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?

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?

Can't use a loop of any sort, only arithmetic and assignment

statements.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

work.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

me.

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) * .......

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?

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.

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.

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!)

I didn't mean implement it in javascript, it is supposed to be

submitted in non-programmed form. Just arithmetic.

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) * .......)

Ok, thanks. Think I got it from here.

Ewww, I think I just did someones homework.........

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.

Ask Your Question

Weekly Poll

Do you agree with Google's decision to shut down Google+?

Discuss in The Lounge

Poll History