分解质因数:输入一个正整数,分解质因数:
如输入: 90 则打印: 90 = 2 * 3 * 3 * 5
get_str = input("请输入一个100以内的正整数,以分解质因数:") get_num = int(get_str) prime_str = "" prime_list = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97] num_list = [] for i in range(25): while True: if get_num % prime_list[i] == 0: num_list.append(prime_list[i]) get_num /= prime_list[i] continue else: break for i in range(len(num_list)): prime_str += str(num_list[i]) if i < len(num_list)-1: prime_str += "*" else: break print("数字%s = %s"%(get_str,prime_str))
质数表