# math question - (divided by 2?)

Rodrigo Bossini

Ranch Hand

Posts: 113

posted 5 years ago

- 0

Is there a smart manner to determine how many times a given integer n number can be divided by two?

For example, 2 can be divided one time by 2.

Any odd number can't be divided by 2.

A number that is a power of 2 can be divided by two exactly log n ( 8 can be divided lg 8 = 3 times by two because 8 = 2^3).

But for the even numbers that are not a power of two, how do I say how many times they can be divided by two?

Thanks.

For example, 2 can be divided one time by 2.

Any odd number can't be divided by 2.

A number that is a power of 2 can be divided by two exactly log n ( 8 can be divided lg 8 = 3 times by two because 8 = 2^3).

But for the even numbers that are not a power of two, how do I say how many times they can be divided by two?

Thanks.

I see wind mills

Mike Simmons

Ranch Hand

Posts: 3028

10

posted 5 years ago

- 0

If you look at the number in binary, then this is determined by the number of zeroes at the end of a number. A number that ends in 1 is not divisible by 2, and for numbers that are divisible by 2, each division by 2 removes one of the trailing zeroes.

That's useful if a number is already

In Java, you can just use Integer.numberOfTrailingZeros().

That's useful if a number is already

*in*binary. Which is generally the case when you're programming. But if you, a human, are looking at a decimal number, then in order to convert to binary, you have to perform a series of divisions. So this trick isn't really any faster for answering your question. If you want to know how many times a number can be easily divided by two, the fastest way is probably to keep dividing by two until you get an odd number.In Java, you can just use Integer.numberOfTrailingZeros().

posted 5 years ago

- 0

Determine how many times 2 goes into the final digit. (For example, if your number is 416, then 2 goes into 6 three times.)

Remove that last digit from the number and multiply by 5. (So 416 becomes 41, and 41 * 5 is 205.)

Add the results. (3 + 205 = 208.)

Remove that last digit from the number and multiply by 5. (So 416 becomes 41, and 41 * 5 is 205.)

Add the results. (3 + 205 = 208.)

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org

Mike Simmons

Ranch Hand

Posts: 3028

10

posted 5 years ago

This looks like some sort of mathematical voo-doo. Where do you get the 5 from?

I see it works, but WHY???

- 0

marc weber wrote:Determine how many times 2 goes into the final digit. (For example, if your number is 416, then 2 goes into 6 three times.)

Remove that last digit from the number and multiply by 5. (So 416 becomes 41, and 41 * 5 is 205.)

Add the results. (3 + 205 = 208.)

This looks like some sort of mathematical voo-doo. Where do you get the 5 from?

I see it works, but WHY???

When you do things right, people won't be sure you've done anything at all.

Mike Simmons

Ranch Hand

Posts: 3028

10

posted 5 years ago

- 0

Removing the last digit of a number is like dividing by ten and losing any remainder. Removing the last digit and then multiplying by five is like dividing by ten and then multiplying by five, which is like multiplying by 5/10 or 1/2. Which is like dividing by two.

What about the lost remainder? Well, that was handled back at the beginning when we looked at the last digit and divided by two, then added the result back on at the end.

So for example:

123456 / 2 = (123450 + 6) / 2 = 123450 / 2 + 6 / 2 = (1234 * 5) + 6 / 2

Here you see everything but the last digit, times 5, plus the last digit, divided by two.

What about the lost remainder? Well, that was handled back at the beginning when we looked at the last digit and divided by two, then added the result back on at the end.

So for example:

123456 / 2 = (123450 + 6) / 2 = 123450 / 2 + 6 / 2 = (1234 * 5) + 6 / 2

Here you see everything but the last digit, times 5, plus the last digit, divided by two.

posted 5 years ago

Cute. But its a lot more work than just doing the division. So while its clever, I'm not sure its "smart" by any sensible criteria.

Even the "remove the last digit" is hard to do on a computer without doing the division by 10, which binary computers do not do well.

If your computer does not have an integer divide instruction, you could do a series of mask/test the lowest bit, and shift right, which is how the division is done in software on those machines.

- 0

marc weber wrote:Determine how many times 2 goes into the final digit. (For example, if your number is 416, then 2 goes into 6 three times.)

Remove that last digit from the number and multiply by 5.

Cute. But its a lot more work than just doing the division. So while its clever, I'm not sure its "smart" by any sensible criteria.

Even the "remove the last digit" is hard to do on a computer without doing the division by 10, which binary computers do not do well.

If your computer does not have an integer divide instruction, you could do a series of mask/test the lowest bit, and shift right, which is how the division is done in software on those machines.

posted 5 years ago

- 0

I just took the original post to be asking for methods

**other than**the straight-forward approach of dividing by 2. The example given (deriving the base 2 log of n, for cases in which n is a power of 2) doesn't seem any easier than simply dividing by 2.*~Joe Strummer*

sscce.org

Teja Saab

Rancher

Posts: 152

Mike Simmons

Ranch Hand

Posts: 3028

10

Teja Saab

Rancher

Posts: 152

Frank Silbermann

Ranch Hand

Posts: 1405

posted 5 years ago

- 0

Suppose you have a list of the powers of two:

2, 4, 8, 16, 32, 64, ....

So think of the problem as a binary search to determine the largest element of the list that is a factor of your number.

Initial endpoints are 2 and the largest element of the list that is smaller than your number.

Pick the power of 2 that is in the middle of your interval, and see whether it divides your number.

If no, then your new interval is your previous low value, and the power of two that is immediately to the left as your high value.

If yes, then the low end of your new interval is the number you just picked, and you keep the previous high end of your interval.

Repeat until your interval contains just one number.

This algorithm may be useful if the initial interval size (possible powers of 2) is very large.

2, 4, 8, 16, 32, 64, ....

So think of the problem as a binary search to determine the largest element of the list that is a factor of your number.

Initial endpoints are 2 and the largest element of the list that is smaller than your number.

Pick the power of 2 that is in the middle of your interval, and see whether it divides your number.

If no, then your new interval is your previous low value, and the power of two that is immediately to the left as your high value.

If yes, then the low end of your new interval is the number you just picked, and you keep the previous high end of your interval.

Repeat until your interval contains just one number.

This algorithm may be useful if the initial interval size (possible powers of 2) is very large.

Mike Simmons

Ranch Hand

Posts: 3028

10

posted 5 years ago

- 0

Frank: nice, that actually can be faster than the standard solution, depending on what we know about the range and distribution of input values. Good thinking.

Teja:

It's obvious that that Rod is talking about being

Teja:

For example, 2 can be divided one time by 2.

Any odd number can't be divided by 2.

It's obvious that that Rod is talking about being

*evenly*divisible. To say "Nowhere is it mentioned that the result of the division shouldn't be a fraction" is to completely ignore what Rod actually*did*say, which contradicts the possibility of fractional results. You might think it should have been phrased differently, but it's disingenuous to simply ignore it.
Evgeniy Bulanov

Greenhorn

Posts: 23

posted 5 years ago

- 0

dividing by 2 is the left shift binary operation

For ex.:

8 = 1000 in the binary form

so, to divide by 2 we should shift it to left one time.

1000(8) >> 1 = 100(4)

100(4) >> 1 = 10(2)

10(2) >> 1 = 1(1)

so, we shift it tree times = (last_value_position_bit - 1) ;)

For ex.:

8 = 1000 in the binary form

so, to divide by 2 we should shift it to left one time.

1000(8) >> 1 = 100(4)

100(4) >> 1 = 10(2)

10(2) >> 1 = 1(1)

so, we shift it tree times = (last_value_position_bit - 1) ;)

Computers are like air conditioners - They stop working properly when you open Windows

Mike Simmons

Ranch Hand

Posts: 3028

10

posted 5 years ago

- 0

Thank you for sharing but Mike gave the answer already. Not in a concrete way but with a great hint.

The best solution would probably be:

The best solution would probably be:

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler

Please correct my English.

I agree. Here's the link: http://aspose.com/file-tools |