找到两个小于X号的最大功率? [英] find largest power of two less than X number?

查看:160
本文介绍了找到两个小于X号的最大功率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这样做
$ b $ pre $ def $ power $ two(n,base = -1):
result = 2 * *如果结果为$ base $ b $< n:
base + = 1
power_two(n,base)
else:
如果结果== n:
打印库
其他:
print base - 1

寻找两个小于X数的最大幂的pythonic方法是什么

编辑
例如:power_two(100)只返回权力

解决方案 / b>找到对数并截断它:

  def power_two(n):
return int( math.log(n,2))


I m doing this

def power_two(n, base = -1):
    result = 2 ** base
    if result < n:
        base += 1
        power_two(n, base)
    else:
        if result == n:
            print base
        else:
            print base - 1

what is the pythonic way to find largest power of two less than X number?

EDIT example: power_two(100) return only the power

解决方案

Find the logarithm and truncate it:

def power_two(n):
    return int(math.log(n, 2))

这篇关于找到两个小于X号的最大功率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆