把一个正整数进行因质分解
程序代码:
def fen_jie(num): # 递归法 for i in range(2,num+1): if i == num: print(i,end="") return elif num % i == 0: print(i,"* ",end="") fen_jie(num // i) return def fen_jie2(num): while num != 1: # 循环判断法 for i in range(2,num + 1): if num % i == 0: print(i,end="") num //= i if num != 1: print(" * ",end="") break if __name__ == "__main__": int_a= 90 print(int_a," = ",end="") fen_jie(int_a) print() print(int_a," = ",end="") fen_jie2(int_a)
输出结果:
90 = 2 * 3 * 3 * 5
90 = 2 * 3 * 3 * 5