python-3.x - How can I make the largest number with most numbers of divisors appear in the output?
问题描述
Title kinda says it all, how do I make a code where I can make the largest number with most divisors appear in the output?
start = int(input("Enter start number: "))
if start <= 0:
print("Invalid Input...")
end = int(input("Enter end number: "))
if end <= 0:
print("Invalid Input...")
divisor = 0
for i in range (start, end):
if end% i == 0:
divisor = i
divisor //= start
print("{0} has {1} divisor.".format(end, divisor))
I expect the output to be like if I input 5 at start and 100 at the end it would look like
Enter start number: 5
Enter end number: 100
96 has 10 divisors.
But instead of 96 it is 100.
解决方案
制作一个函数,它将返回所选数字的除数。这样你就可以找到一个具有最大除数的数字并打印它:
start = int(input("Enter start number: "))
if start <= 0:
print("Invalid Input...")
end = int(input("Enter end number: "))
if end <= 0:
print("Invalid Input...")
def divisors(n):
cnt = 0
for i in range(2,int(n**0.5)+1):
if n%i == 0:
cnt += 2
return cnt
current_max, current_cnt = 0, 0
for i in range(start, end):
n = divisors(i)
if n >= current_cnt:
current_cnt = n
current_max = i
print("{0} has {1} divisor.".format(current_max, current_cnt))
打印(用于输入5
和100
):
96 has 10 divisor.
推荐阅读
- clojure - 宏观构成
- google-cloud-platform - Cloud Function + Cloud Build:如何在构建时将文件添加到函数中
- dplyr - 删除 dplyr 中部分相同的行
- angularjs - 我正在使用动态面板。我使用了“折叠”按钮。问题是,折叠按钮仅适用于第一个面板
- r - 需要安排可以在 2 小时间隔内运行的 R 脚本
- javascript - react-native-calendar :使用弹出窗口应用自定义多标记
- java - 如何为用户定义的类实现通用比较器
- php - 想要在不刷新页面的情况下获取 php sql 查询的下拉值
- docker - 詹金斯不在奴隶上运行工作
- shell - 删除匹配的单词,但在文件中保留原始单词