水仙花数是指一个三位数,其各位数字的3次方和等于该数本身,用Python编写按照从小到大的顺序输出所有的3位数水仙花数,请用英文逗号分隔开结果,最后一个水仙花数之后不出现英文逗号
时间: 2024-10-23 14:05:00 浏览: 38
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身
5星 · 资源好评率100%
A narcissistic number is a three-digit number where the sum of its digits raised to the power of 3 equals the original number itself. Here's a Python code snippet that will generate and output all such numbers in ascending order, without commas between them once the sequence is complete:
```python
# Function to check if a number is a narcissistic number
def is_narcissistic(num):
return num == sum(int(digit) ** 3 for digit in str(num))
# Generate and collect narcissistic numbers within the range
narcissists = []
for i in range(100, 1000): # Only consider three-digit numbers
if is_narcissistic(i):
narcissists.append(i)
# Output the result without commas after the last number
result = ', '.join(map(str, narcissists))
if len(result) > 0:
print(result[:-1]) # Remove the trailing comma
else:
print("No narcissistic numbers found.")
```
阅读全文