permaculture playing cards*
The moose likes Meaningless Drivel and the fly likes math question - (divided by 2?) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Other » Meaningless Drivel
Bookmark "math question - (divided by 2?)" Watch "math question - (divided by 2?)" New topic
Author

math question - (divided by 2?)

Rodrigo Bossini
Ranch Hand

Joined: Jul 03, 2009
Posts: 113
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.


I see wind mills
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
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 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().
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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


"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

Joined: Mar 05, 2008
Posts: 3007
    
    9
Yeah - that's much easier than simply dividing by two.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11

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

Joined: Mar 05, 2008
Posts: 3007
    
    9
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.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1660
    
  11



Holy crap.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
I figured it was meant as a joke.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.
Teja Saab
Rancher

Joined: Mar 08, 2010
Posts: 152
Rod Bossini wrote:Is there a smart manner to determine how many times a given integer n number can be divided by two?


The answer is "infinite". Nowhere is it mentioned that the result of the division shouldn't be a fraction.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Oh, please. The original post was pretty clear on this with its examples.
Teja Saab
Rancher

Joined: Mar 08, 2010
Posts: 152
Mike Simmons wrote:Oh, please. The original post was pretty clear on this with its examples.


Frank Silbermann
Ranch Hand

Joined: Jun 06, 2002
Posts: 1387
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.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
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:

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

Joined: Dec 19, 2005
Posts: 23
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) ;)

Computers are like air conditioners - They stop working properly when you open Windows
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
Wow - it's almost as though if you look at the number in binary, the question is answered by the number of zeroes at the end of the number. How interesting.
Naren Chivukula
Ranch Hand

Joined: Feb 03, 2004
Posts: 576

Here is a simple program which does the job. Hope you are looking something like this, if not let us know.
Note: You can change the code so that variable n value is provided at the runtime.


Cheers,
Naren
(OCEEJBD6, SCWCD5, SCDJWS, SCJP1.4 and Oracle SQL 1Z0-051)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

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:


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
 
wood burning stoves
 
subject: math question - (divided by 2?)