pythonfor-loopwhile-loophamming-codehamming-numbers# 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()
```

Hi, I need to print the hamming numbers but I can only do it with the if loop. How can I do it with a for or while loop?

Solution

Something like this?

```
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 Jinja2 LaTeX Table
- Getting attributes of a class
- How can I print many significant figures in Python?
- How to allow list append() method to return the new list
- Calculate Last Friday of Month in Pandas
- Python type hint for Iterable[str] that isn't str
- How to iterate over a list in chunks
- How to exit the entire application from a Python thread?
- Running shell command and capturing the output
- How do I pass a variable by reference?
- Convert range(r) to list of strings of length 2 in python
- How can I get the start and end dates for each week?
- how to use send_message() in python-telegram-bot
- Python conditional replacement based on element type
- How can I count the number of items in an arbitrary iterable (such as a generator)?
- Find longest consecutive range of numbers in list
- Insert text in braces with asyncpg
- How does one put a link / url to the web-site's home page in Django?
- How to determine if a path is a subdirectory of another?
- Custom Keybindings for Ipython terminal
- FastAPI asynchronous background tasks blocks other requests?
- How to make sure that information from one file is duplicated into several text documents, without specific lines
- Installing a Python environment with Anaconda
- sklearn pipeline model predicting same results for all input
- Brew command not found after installing Anaconda Python
- How to get an XPath from selenium webelement or from lxml?
- Pipe PuTTY console to Python script
- How to align the axes of a figure in matplotlib?
- Persist ParentDocumentRetriever of langchain
- How to reset index in a pandas dataframe?