python - 如何在python中获取具有给定数量因子的可能数字列表?
问题描述
我可以得到给定数字的因子和因子数:
def all_factors(x):
factors = []
for i in range(1, x + 1):
if x % i == 0:
factors.append(i)
return factors
print(all_factors(320))
print(len(all_factors(320)))
它提供以下输出:
[1、2、4、5、8、10、16、20、32、40、64、80、160、320]
14
但是,我该如何做相反的事情呢?例如:如果我的因子数 = 4,则可能的列表必须是 [6, 10, 14, 21, ...] 我们可以限制列表中的最大整数。
解决方案
尝试:
n =int(input("Enter the limit: "))
factno = int(input("No of factors: ")) # here 4
listing = []
for i in range(1,n+1):
#Your code here to find factors and to add then to a list named "factors'
factors = []
for j in range(1, i + 1):
if i % j == 0:
factors.append(j)
if len(factors) == factno:
listing.append(i)
print(listing)
推荐阅读
- php - Typo3 10 方面添加到上下文时不保存
- ruby-on-rails - 大型 Rails 项目(例如 Stripe、Shopify 的)如何处理事件回调?
- big-o - 如果分母的增长率大于分子,那么它是theta(1),这是真的吗?
- c# - C# HttpWebRequest - 向服务器发送巨大的流
- python - 如何从 python 中的布尔值列表中进行所有可能的组合?
- linux-kernel - Yocto 多个内核包
- typescript - 为什么 Typescript 动态元组类型显示自动完成/智能感知中的所有选项?
- java - 为什么我的代码在请求用户输入时会跳到 else 语句?
- sql - 如何在 Snowflake SQL 中同时聚合并取一列的最后一个值?
- css - 如何使用变换原点让 svg 元素从中心旋转?