這是我創建的用於查找2因子的最大功率的代碼。我不認爲這是100%正確的,因爲我始終得到2作爲答案。我需要一些幫助來解決這個問題。我在編程方面全新。找到2的最大力量?
我的代碼:
def largestPowerOfTwoThatIsAFactorOf(num):
factor = 2
while not(num > 0):
factor = factor + 1
return factor
print(largestPowerOfTwoThatIsAFactorOf(4))
print(largestPowerOfTwoThatIsAFactorOf(15))
print(largestPowerOfTwoThatIsAFactorOf(120))
#For any odd integer, largest power of 2 that’s a factor is 1.
'while not(num> 0)'表示:「只要數字爲0或更小就運行循環」。這不會爲正數運行循環。對於0,它將執行一次循環,並使函數返回3.這不是兩個冪。在**將代碼盲目地嵌入到計算機中之前,您應該考慮**算法**。 – 2014-11-04 06:13:30
不要從'factor = 2'開始 – hjpotter92 2014-11-04 06:14:08
當你是一個完整的初學者時,很難,我盡我所能@TheParamagneticCroissant – 2014-11-04 13:18:54