python实现因式分解求解

编程实现:对于用户从键盘输入的大于1的自然数,对其进行因式分解。例如,10=25,60=2235

>>> def func(n):
    i, j = 2, n
    res = []
    while j!=1 or i<j:
        if j%i==0:
            res.append(str(i))
            j//=i
        else:
            i+=1
    return str(n)+' = '+'X'.join(res)

>>> x = int(input())
60
>>> print(func(x))
60 = 2X2X3X5
>>> x = int(input())
10
>>> print(func(x))
10 = 2X5
>>> x = int(input())
7
>>> print(func(x))
7 = 7
>>>