|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
自己写了一个用于分解质因数的程序,优化了一下速度,使16位以内的数的分解速度,限制在几百毫秒以内
- import time as t
- def prime_factorization(i):
- i = int(i)
- t1 = t.time()
- x = []
- y = []
- p = i
- j = 2
- heshu = False
- while i != 1.0 and j <= i:
- if i % j == 0:
- heshu = True
- if heshu == True and j > i ** 0.5:
- x.append(int(i))
- y.append(1)
- break
-
- if i % j != 0:
- j += 1
- if j > p ** 0.5 and heshu == False:
- print('\n\t您输入的数字是质数!', end='')
- break
- continue
-
- count = 0
- x.append(j)
- while i % j == 0:
- count += 1
- i /= j
-
- y.append(count)
- #print(i, i ** 0.5)
- if heshu == True:
- print('\n\t质因数分解: '+str(p)+' = ', end='')
- for m in range(len(y)):
- print(str(x[m])+'^'+str(y[m]), end='')
- if m < len(y) - 1:
- print(' * ', end='')
- t2 = t.time()
- deltat = round(t2 - t1, 3)
- print()
- print('\t跑完程序所花时间: '+str(deltat)+' 秒。\n')
- while True:
- i = input('请输入要分解的正整数:')
- try:
- i = int(i)
- except ValueError:
- print('您输入的不是数,请重新输入!')
- continue
- if int(i) < 0:
- i *= -1
- prime_factorization(i)
-
-
复制代码
运行结果见截图
|
-
iPad上的运行效果
|