【发布时间】:2018-08-11 07:17:01
【问题描述】:
这是我在给定限制下生成所有汉明数(又名 5 平滑数)的代码。 https://en.wikipedia.org/wiki/Smooth_number
在数论中,光滑(或易碎)数是一个整数,它完全分解为小素数。例如,7 平滑数是质因数最多为 7 的数,因此 49 = 7^2 和 15750 = 2 × 32 × 53 × 7 都是 7 平滑数,而 11 和 702 = 2 × 33 × 13 不是
我知道生成数字的其他方法,但是我首先提出的函数对我来说很有意义,并且想知道如何将其扩展为 7-smooth、11-smooth、...、n-smooth-numbers。有没有更简单的方法来更改我的函数以循环低于限制的素数而不添加非常相似的代码行?
import math
def HammingFiveUnder(limit):
hammings = []
exp2 = int(math.log(limit, 2))
exp3 = int(math.log(limit, 3))
exp5 = int(math.log(limit, 5))
for i in range(0, exp2+1):
for j in range(0, exp3+1):
for k in range(0, exp5+1):
poss_ham = 2**i * 3**j * 5**k
if poss_ham <= limit:
hammings.append(poss_ham)
return sorted(hammings)
print(HammingFiveUnder(100))
【问题讨论】:
-
为什么不反过来:寻找低于限制的素数分解,并过滤掉那些素因数过大的?
-
找到所有数字的素数分解是一个计算难题
标签: python number-theory