python - 因式分解,请帮我看看d出了什么问题,我是刚学习的新手
问题描述
在此处输入图像描述
输入:20 17 999997 输出:2^2 * 5 17 757 * 1321
我的代码:
a=int(input())
#find the factors first
for i in range(2,a+1):
s=0
b=a
d=0
#see if it is a prime number
if a%i==0:
for x in range(1,i+1):
if a%x==0:
d=d+x
if (d-1)/i==1:
d=0
print(i)
else:
s=0
b=a
d=0
continue
d=0
#I will see how many prime numbers
while(b>0):
if (b/i)%1==0:
s=s+1
b=b/i
else:
b=0
if b==1:
b=0
print(s)
我会先找因数,然后看是不是素数。如果是这样,我会看看它是多少个素数
如果我输入 12 ,它会输出 2 2
解决方案
我相信您需要的是以下输出。
import math
a=int(input())
while (a % 2 == 0):
print(2)
a = int(a/2)
while (a % 3 == 0):
print(3)
a = int(a/3)
for i in range(5,math.ceil(math.sqrt(a)),6):
while (a % i == 0):
print(i)
a = int(a / i)
while (a % (i + 2) == 0):
print(i + 2)
a = int(a / (i + 2))
if (a > 3):
print(a)
这将为您提供给定数字的主要因素。据我所知,这就是您要寻找的东西。
推荐阅读
- python - 如果文件将对称链接,那么同时编辑的结果应该是什么?
- firebase - Flutter 应用程序的活动跟踪(iOS 和 Android)
- python - 如何随机打乱列表并以 5 项为一组提供
- python - 我需要如何训练 LSTM 网络才能预测不同长度的数组?
- list - 如何更新 Haskell 中的列表元素
- node.js - “nvm use 16.13.0”将返回此错误“退出状态 145:目录不为空”。
- git - SSH 与 Git 克隆
- python - 尝试创建一个执行简单统计计算的类,ValueError: invalid literal for int() with base 10: ''
- azure - SharePoint Online CData JDBC Databricks AD 已验证调用获取 HTTP 协议错误。500 服务器错误
- sql - AWS Timestream 查询不同的测量值