500以下最大输入功率

时间:2018-09-05 16:42:58

标签: python function math input while-loop

我正试图找到最大的输入数字幂,该数字小于500,但我的代码仍无法正常工作。我在做什么错?谢谢!!

n = int(input())
def function (n):
    x = 1
    y = n**x
    while y < 500:
        x += 1
        y = n**x
    return y
print function (n)

6 个答案:

答案 0 :(得分:1)

解决这个问题的一种简单方法是使用对数。

import math

def function(n):

    # If n is 500 then the solution to the equation is an integer and it will not be rounded down 
    # using the following code
    if n == 500:
        return 1

    # Find the solution to the equation and round down to find the largest integer power
    power = int(math.log(500) / math.log(n))
    return n**power

答案 1 :(得分:0)

您返回的结果超过500,则必须减去1以保持低于500:

修复:

Lines <- "
ID month Price      
1   1    0.1   
1   2    0.2  
1   3    0.3  
2   1    0.1   
2   2    0.2  
2   3    0.2"

DF <- read.table(text = Lines, header = TRUE)

5的输出:

n = int(input())
def function (n):
    x = 1
    y = n**x
    while y < 500:
        x += 1
        y = n**x

    # now we are over 500 - lets step back by 1  
    return n,x-1,n**(x-1)  # return a tuple of all numbers needed


number, power, result = function (n)
print("{} ** {} = {}".format(number,power,result))

答案 2 :(得分:0)

首先计算临时y的值,如果新值大于y,则返回上一个(有效的<500 500值):

def function (n):
    x = 1
    y = 0
    while True:
        x += 1
        temp_y = n**x
        if temp_y > 500:
            return y
        y = temp_y

while True条件避免多次计算y。如果未找到任何内容,则该函数返回0

更好的重写将避免使用幂函数,而将使用以前累积的结果(使用整数乘法,而不是浮点数幂,当数字变大时可能不准确)

def function (n):
    y = 0
    temp_y = n  # maybe start at 1 ?
    while True:
        temp_y *= n
        if temp_y > 500:
            return y
        y = temp_y

这节省了使用x,更短,更高效的代码的需要。

答案 3 :(得分:0)

您首先要递增x。进行计算后,尝试检查该值,然后进入循环。修正了您的代码。添加了用于将参数传递为0的代码

n = int(input())

def function (n):
    x = 1
    if not n:
        return 0

    while  n**x < 500:
        y = n**x
        x += 1
    return y
print function (n)

答案 4 :(得分:0)

实际上,您不必使用while循环,因为我们可以安全地同意运行一次循环,直到所有数字(除1 ** 100000 = 1中的1以外)都可以像2 ** 100 = 1267650600228229401496703205376一样运行

您不应使用function,因为有一个内置的名为“ function”的函数
您不应该从x = 1开始,因为您永远不知道n何时会达到500 +

n = int(input())

def f(n):
    for x in range(100):
        if n ** x > 500:
            return n ** (x - 1)

print(f(n))

如果您希望所有n ** x均小于500:

def f(n):
    arr = []

    for x in range(100):
        if n ** x < 500: arr.append(n ** x)
        else: break

    return arr

或单线:

def f(n): return [n ** x for x in range(100) if n ** x < 500]

希望这会有所帮助!

答案 5 :(得分:0)

四种方法:一步法,一种迭代法,另一种迭代法避免过多的乘法,二进制搜索以非常大的限制工作

import math

def maxpow(n, limit):
    return n**math.floor(math.log(limit) / math.log(n))

def maxpowit(n, limit):
    x = 1
    while (x * n < limit):
        x *= n
    return x

def maxpowit2(n, limit):
    x, y = 1, n
    while (y < limit):
        x, y = y, y * n
    return x

def maxpowbs(n, limit):
    stack = []
    x, y = 1, n
    while (y < limit):
        stack.append(y)
        x, y = y, y * y
    #here stack holds values n^(2^i)
    #like [2, 4, 16, 256, 65536, 4294967296]  
    for i in reversed(range(len(stack)-1)):
        y = stack[i]
        if (x * y < limit):
            x *= y
    return x


print(maxpow(2, 500), maxpowit(2, 500))
>>> 256 256

print(maxpowbs(2, 1152921504606846997))#2^60 + xxx
>>>1152921504606846976   #2^60 exactly
相关问题