如何在 while 或 for 循环中转换海明数代码 python
How can I convert hamming number code in a while or for loop python
def is_hamming_numbers(x):
if x == 1:
return 1
if x % 2 == 0:
return is_hamming_numbers(x/2)
if x % 3 == 0:
return is_hamming_numbers(x/3)
if x % 5 == 0:
return is_hamming_numbers(x/5)
return 0
def hamming_numbers_sequence(x):
if x == 1:
return 1
hamming_numbers_sequence(x-1)
if is_hamming_numbers(x) == True:
print("%s" % x, end=' ')
print(is_hamming_numbers(7))
print(is_hamming_numbers(1))
hamming_numbers_sequence(24)
print()
你好,我需要打印汉明数,但我只能用 if 循环来完成。我怎样才能用 for 或 while 循环来做到这一点?
是这样的吗?
def is_hamming_numbers(x: int) -> bool:
if x == 1:
return True
if x % 2 == 0:
return is_hamming_numbers(x/2)
if x % 3 == 0:
return is_hamming_numbers(x/3)
if x % 5 == 0:
return is_hamming_numbers(x/5)
return False
for n in range(1, 25):
if is_hamming_numbers(n):
print(n)
另一种检查和打印海明数的方法,希望对您有所帮助。
Python代码:
# Hamming number has only prime factors as 2,3,5
def is_hamming_number(n):
factors = [];
hammingNumberCheck = {1,2,3,5,n}
for i in range(1, n + 1):
if n % i == 0:
factors.append(i)
factors_set = set(factors)
if(factors_set.issubset(hammingNumberCheck)):
return ":" + str(n) + ", is a hamming number"
else:
return ":" + str(n) + ", is not a hamming number"
print (is_hamming_number(10))
Ans-:10,是一个海明数
这个实现使用了一个优先级队列,用一个堆实现。
from heapq import heappush, heappop
from itertools import islice
def hamming_number_seq():
heap = [1]
while True:
h = heappop(heap)
while heap and h == heap[0]:
heappop(heap)
for m in [2,3,5]:
heappush(heap, m*h)
yield h
print(*(islice(hamming_number_seq(), 15))) # This prints the first 15 numbers of the sequence.
输出:
1 2 3 4 5 6 8 9 10 12 15 16 18 20 24
def is_hamming_numbers(x):
if x == 1:
return 1
if x % 2 == 0:
return is_hamming_numbers(x/2)
if x % 3 == 0:
return is_hamming_numbers(x/3)
if x % 5 == 0:
return is_hamming_numbers(x/5)
return 0
def hamming_numbers_sequence(x):
if x == 1:
return 1
hamming_numbers_sequence(x-1)
if is_hamming_numbers(x) == True:
print("%s" % x, end=' ')
print(is_hamming_numbers(7))
print(is_hamming_numbers(1))
hamming_numbers_sequence(24)
print()
你好,我需要打印汉明数,但我只能用 if 循环来完成。我怎样才能用 for 或 while 循环来做到这一点?
是这样的吗?
def is_hamming_numbers(x: int) -> bool:
if x == 1:
return True
if x % 2 == 0:
return is_hamming_numbers(x/2)
if x % 3 == 0:
return is_hamming_numbers(x/3)
if x % 5 == 0:
return is_hamming_numbers(x/5)
return False
for n in range(1, 25):
if is_hamming_numbers(n):
print(n)
另一种检查和打印海明数的方法,希望对您有所帮助。
Python代码:
# Hamming number has only prime factors as 2,3,5
def is_hamming_number(n):
factors = [];
hammingNumberCheck = {1,2,3,5,n}
for i in range(1, n + 1):
if n % i == 0:
factors.append(i)
factors_set = set(factors)
if(factors_set.issubset(hammingNumberCheck)):
return ":" + str(n) + ", is a hamming number"
else:
return ":" + str(n) + ", is not a hamming number"
print (is_hamming_number(10))
Ans-:10,是一个海明数
这个实现使用了一个优先级队列,用一个堆实现。
from heapq import heappush, heappop
from itertools import islice
def hamming_number_seq():
heap = [1]
while True:
h = heappop(heap)
while heap and h == heap[0]:
heappop(heap)
for m in [2,3,5]:
heappush(heap, m*h)
yield h
print(*(islice(hamming_number_seq(), 15))) # This prints the first 15 numbers of the sequence.
输出:
1 2 3 4 5 6 8 9 10 12 15 16 18 20 24