首页 > 解决方案 > 因式分解,请帮我看看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

在此处输入链接描述

标签: pythonfor-loopfactorization

解决方案


我相信您需要的是以下输出。

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)

这将为您提供给定数字的主要因素。据我所知,这就是您要寻找的东西。


推荐阅读