开发者

Function that finds how many times n can be divided in half

开发者 https://www.devze.com 2023-02-12 05:49 出处:网络
Basically, I need a function that will divide n by two and return the number of times it can be done.

Basically, I need a function that will divide n by two and return the number of times it can be done.

Coding so far:

def div(n):
    while n >= 0:
        n / 2
    return n

I know for a fact that I have to use the while loop, but I'm not confident in my third line of coding. What am I doing wrong?

Examp开发者_JS百科les:

>>> div(4)
2
>>> div(7)
2


An integer n can be divided by 2: floor(log(n)/log(2)) times.


/ does not perform assignment. Since you're returning n and not changing its value, I'd think you should start there. Your other hints are = and %.


(Note that I'm taking your examples as defining the expected behaviour of the function. As Tomas and Carl noted, the description isn't currently particularly clear.)

Recent versions of Python actually provide a bit_length() method on integers that makes it easy to find out the answer.

While that doesn't really help you with your homework question (it gives you the answer, but it doesn't help you understand why that's the answer), you can use it to create a reference function to compare with your actual answer:

def expected(n):
    return n.bit_length() - 1

>>> expected(4)
2
>>> expected(7)
2
>>> expected(33)
5

A few points to consider:

  • your loop condition isn't correct, as repeated division will never reduce a value below zero. Given the example data, your terminating condition needs to be something different. Consider the answers you would expect for inputs of 0, 1 or 2.
  • you need to be changing the value of n on each pass through the loop. Otherwise, your loop will never end.
  • is n really the value you want to be returning? Perhaps there is something else you should be tracking as you go around the loop that will become your return value (a closer look at some of the other suggested answers should help with this point).


Divisibility is defined as follows: one integer is divisible by another integer only if the result of that division is also an integer (not a fraction, no remainder).

See the mathematical definition.

For example, 15 is divisible by 3 and 5 but not divisible by 2.
Given this definition, the highly-rated answer above, floor(log(n)/log(2) is completely wrong.

For example:
3^10= 59,049
This number is odd and only divisible by 3.
So count-2-divisions(3^10) = 0 The expression floor(log(3^10)/log(2)=15.


The internal function divmod is the key:

divmod(20,3)
>>> (6, 2)

divmod(20,3)[0]
>>> 6


I see at least two ways of doing it:

Bitwise operation (shift):

i = 8
result = 0
while i > 1:
    i = i >> 1
    result = result + 1
print result

and

i = 8
result = 0
while i > 1:
    i = i / 2
    result = result + 1
print result


Here's a function that counts how many times a number n can be divided by another number p using the modulo:

>>> def dividible(n, p):
...     i = 0
...     while n%p==0:
...             n /= p
...             i += 1
...     return i
...
>>> dividible(16, 2)
4
>>> dividible(15, 2)
0
>>> dividible(8, 2)
3
>>> dividible(100, 2)
2


The interpretation is: How many times can you halve it (discarding remainder) until 0; assumes 32-bit integers?

def div(n):
    while n & (n - 1) > 0:
        n = n & (n - 1)
    a = [0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9]
    tmp = (n * 0x077CB531) % 0x100000000
    if tmp > 0xd0000000:
        tmp += -0x100000000
    return a[tmp >> 27]

Sample output:

   >>>print div(100)
   6
   >>>print div(1000)
   9
   >>>print div(1025)
   10

etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消